i read some contributions about this but nothing helped.
I try to make a little application with a gui,
but the problem is, if i click the button, no animation appears and nothing happens. I hope you can help me.
That's the code:
public class StartFrame extends JFrame{
JTextField eingabe;
JLabel inhalt;
JButton button;
JCheckBox fett;
JCheckBox kursiv;
JCheckBox groß;
JPanel panel;
StartFrame(int sizeWidth, int sizeHeight, String title){
setSize(sizeWidth, sizeHeight);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(title);
setLocationRelativeTo(null);
setLayout(null);
inhalt = new JLabel("TeXt");
inhalt.setBounds(0, 0, 500, 60);
Font font = inhalt.getFont().deriveFont(Font.ITALIC, 15);
inhalt.setFont(font);
inhalt.setToolTipText("Das ist ein Text");
add(inhalt);
button = new JButton("HIER");
button.setBounds(100, 10, 100, 50);
button.addActionListener(new ClickListener());
button.setEnabled(false);
add(button);
eingabe = new JTextField();
eingabe.setBounds(300, 50, 150, 25);
eingabe.addCaretListener(new SchreibkopfListener());
add(eingabe);
panel = new JPanel();
panel.setLayout(null);
panel.setBounds(10, 200, 150, 100);
add(panel);
fett = new JCheckBox("Fett");
fett.setBounds(0, 0, 150, 25);
fett.addItemListener(new FettListener());
panel.add(fett);
kursiv = new JCheckBox("Kursiv");
kursiv.setBounds(0, 25, 150, 25);
panel.add(kursiv);
groß = new JCheckBox("Groß");
groß.setBounds(0, 50, 150, 25);
panel.add(groß);
setVisible(true);
}
private class ClickListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent arg0) {
inhalt.setText(eingabe.getText());
}
}
private class SchreibkopfListener implements CaretListener{
#Override
public void caretUpdate(CaretEvent arg0) {
String inTextField = eingabe.getText();
inTextField = inTextField.trim();
if(inTextField.isEmpty()){
button.setEnabled(false);
}else{
button.setEnabled(true);
}
}
}
private class FettListener implements ItemListener{
#Override
public void itemStateChanged(ItemEvent arg0) {
if(fett.isSelected()){
Font font = inhalt.getFont().deriveFont(Font.BOLD, 15);
inhalt.setFont(font);
}else{
Font font = inhalt.getFont().deriveFont(Font.ITALIC, 15);
inhalt.setFont(font);
}
}
}
}
Here's a wonderful lesson into why null layouts suck (and you should stop using them)
Let's just take a little look at these lines...
nhalt = new JLabel("TeXt");
inhalt.setBounds(0, 0, 500, 60);
inhalt.setToolTipText("Das ist ein Text");
//...
button = new JButton("HIER");
button.setBounds(100, 10, 100, 50);
So, the label starts at 0x0 and expands through 500x60, okay, the button starts at 100x10 and expands through 100x50, this means that the label and the button actually collide
Now, I know what you're thinking, but I add the button after the label, but this isn't how components actually get painted/respond to events.
Components are painted in reverse order of how they are added, this means that the label actually resides OVER the button (in terms of the Z-order), this also means that the label receives events BEFORE the button, this is important because...
inhalt.setToolTipText("Das ist ein Text");
installs a MouseListener on the JLabel, which consumes ALL mouse events, preventing the button from been notified.
So, long answer short, don't use null layouts (and take the time to better understand how the API actually works :P)
Related
I know the code is bad and easy. I am a starter and need some help. I want to add a second button to my code. I feel like I changed the coordinates of the button so it doesn't "fall" on the first one. Any suggestions? (Also, .this is a bit unknown to me so I just copy - pasted it hoping it doesnt affect the second button. I feel like it has to do with .this and the coordinates of the button I entered).
import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI extends Frame implements ActionListener
{
static Operand op;
TextField display;
Button button0;
Button button1;
public CalculatorGUI(String title, Operand op)
{
super(title);
CalculatorGUI.op = op;
this.setLayout(null);
this.setFont(new Font("TimesRoman", Font.PLAIN, 14));
this.setBackground(Color.blue);
button0 = new Button("0");
button0.setBounds(64, 265, 35, 28);
button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button0.setForeground(Color.blue);
button0.setFocusable(false);
button0.addActionListener(this);
this.add(button0);
this.setSize(283,320);
this.setLocation(40,30);
this.setVisible(true);
this.setResizable(false);
button1 = new Button("1");
button1.setBounds(64, 230, 35, 28);
button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button1.setForeground(Color.blue);
button1.setFocusable(false);
button1.addActionListener(this);
this.add(button1);
this.setSize(283,320);
this.setLocation(40,30);
this.setVisible(true);
this.setResizable(false);
this.addWindowListener(new CloseWindowAndExit());
display = new TextField("");
display.setEditable(false);
display.setBounds(13, 55, 257, 30);
this.add(display);
}
class CloseWindowAndExit extends WindowAdapter
{
public void windowClosing(WindowEvent closeWindowAndExit)
{
System.exit(0);
}
}
#Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button0)
{
display.setText("Button0 is pressed");
CalculatorGUI.op.addDigit('0');
}
}
}
Do not add buttons directly to the main frame. Instead, create another panel and add the buttons to it:
Panel newPanel = new Panel(new GridBagLayout());
Button button0 = new Button("BUTTON_0");
button0.setBounds(10, 265, 30, 30);
button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button0.setForeground(Color.blue);
button0.setFocusable(false);
Button button1 = new Button("BUTTON_1");
button1.setBounds(10, 265, 30, 30);
button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
button1.setForeground(Color.blue);
button1.setFocusable(false);
newPanel.add(button0);
newPanel.add(button1);
this.add(newPanel);
I am a creating a certain project in java.
I added a button to sign up the user on one jframe and on button click first jframe dispose and second opens up where i have added a back button to go to first frame and login button. When login button is clicked a jdialog appears.
(I extends the jdialog class)
I have found out that if i click back button and then again click on sign up button, second jframe opens up which is normal but when i click login two instances of jdialog appears.
Here is the code for first JFrame button
frontsignbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new SignUpNew().buildDesign();
frontframe.dispose();
}
});
And for second
login.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Login();
}
});
And JDialog code
public class Login extends JDialog {
private JLabel usernametext = new JLabel("User Name");
private JLabel userpasswordtext = new JLabel("Password");
private JLabel message = new JLabel();
private JTextField userfield = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton submit = new JButton("Submit");
{
setTitle("Login");
setLayout(null);
setSize(400, 300);
setLocation(400, 300);
setVisible(true);
setResizable(false);
usernametext.setBounds(20, 20, 80, 10);
userpasswordtext.setBounds(20, 80, 80, 10);
userfield.setBounds(150, 20, 100, 20);
password.setBounds(150, 80, 100, 20);
submit.setBounds(75, 140, 100, 30);
message.setBounds(140, 240, 230, 30);
add(usernametext);
add(userpasswordtext);
add(userfield);
add(password);
add(submit);
add(message);
}
Any idea why this happens. I want to fix this.
I am creating a program in java, similar to Quizlet.
I created a class for each Jpanel which has the layout set to null. The only class that doesn't have the null layout is the one that manages and contains all the JPanels, which has the CardLayout.
Anyways, everything was working fine, the problem occurred in one of my classes that contains a Jpanel that loads each quiz. I called it LoadMenu.
The GUI was working just fine, and I just wanted to check out the box layout, so I switched it from null layout to box. Then I realized all of my components, except for one, disappeared on that layout. Now when I changed the layout back to null, everything is still gone, and the JPanel all together completely disappeared. I hit Control-Z and undo any changes, but it's still gone.
Is there anyway I can restore it to what it looked like before?
Thank you
Not sure if this helps but this is the loadMenu class
public class LoadMenu extends JPanel implements ActionListener {
private QuizManager manager;
private FileManager file = new FileManager();
private JComboBox comboBox;
private JButton nextButton;
private JButton refreshButton;
private JButton backButton;
private ProblemSet problemSetList;
LoadMenu(ProblemSet newProblemSetList, QuizManager newManager){
manager = newManager;
problemSetList = newProblemSetList;
JPanel loadMenu = new JPanel();
loadMenu.setLayout(null);
loadMenu.setBounds(1, 0, 681, 426);
add(loadMenu);
loadMenu.setLayout(null);
loadMenu.setBounds(1, 0, 681, 426);
JLabel selectQuizLabel = new JLabel("Select Quiz");
selectQuizLabel.setFont(new Font("Times New Roman", Font.BOLD, 20));
selectQuizLabel.setBounds(261, 104, 110, 31);
loadMenu.add(selectQuizLabel);
comboBox = new JComboBox();
comboBox.setBounds(274, 194, 153, 22);
loadMenu.add(comboBox);
nextButton = new JButton("Next");
nextButton.setBounds(530, 370, 139, 43);import junit.framework.TestCase;
import junit.framework.TestCase;
loadMenu.add(nextButton);
nextButton.addActionListener(this);
listQuizNames();
refreshButton = new JButton("Refresh");
refreshButton.addActionListener(this);
refreshButton.setBounds(241, 229, 139, 31);
loadMenu.add(refreshButton);
backButton = new JButton("Back");
backButton.addActionListener(this);
backButton.setBounds(12, 370, 139, 43);
loadMenu.add(backButton);
}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(src == nextButton){
manager.startQuiz((String)comboBox.getSelectedItem());
}
else if(src == refreshButton){
listQuizNames();
}
else{
manager.showNextCard("startMenu");
}
}
public void listQuizNames(){
ArrayList<String> quizList = new ArrayList<String>();
file.readQuizNames(quizList);
comboBox.setModel(new DefaultComboBoxModel(quizList.toArray()));
}
}
Try adding the panel to a JFrame, it might help
JFrame frame = new JFrame();
frame.add(loadmenu);
In the main function or the constructor - suit yourself
setVisible(true);
Hope it works!!
I am working on a little menu program with clickable buttons and an image that changes based on button clicks. If I click a button I get a shadow of the button at the bottom where I change the JLabel text. I cannot figure it out for the life of me. Any advice would be greatly appreciated. Visuals below...thanks
public class SampleGUI
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
System.out.println("Created GUI on EDT? " +
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Robert's VICI Prototype");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.pack();
f.setVisible(true);
}
}
class MyPanel extends JPanel
{
// Fields
Image imageDisplayed;
JLabel status;
// Methods
public MyPanel()
{
setBorder(BorderFactory.createLineBorder(Color.BLACK));
setLayout(null);
JLabel title = new JLabel("CS380 TEAM 5 VICI Prototype");
title.setFont(new Font("Tahoma", Font.BOLD, 20));
title.setBounds(425, 10, 400, 40);
add(title);
status = new JLabel("Please click an option above.");
status.setFont(new Font("Tahoma", Font.BOLD, 14));
status.setBounds(425, 740, 400, 40);
add(status);
JButton choice1 = new JButton("Search Class");
choice1.setBounds(50, 50, 150, 40);
add(choice1);
JButton choice2 = new JButton("Add Class");
choice2.setBounds(225, 50, 150, 40);
add(choice2);
JButton choice3 = new JButton("Drop Class");
choice3.setBounds(400, 50, 150, 40);
add(choice3);
JButton choice4 = new JButton("Verify Reg Hold");
choice4.setBounds(575, 50, 150, 40);
add(choice4);
JButton choice5 = new JButton("Verify Reg Date");
choice5.setBounds(750, 50, 150, 40);
add(choice5);
JButton choice6 = new JButton("Schedule Advisor");
choice6.setBounds(925, 50, 150, 40);
add(choice6);
choice6.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Schedule Advisor button pressed.");
status.setText("Choose a date.");
imageDisplayed = new ImageIcon("C:\\Temp\\sa01.jpg").getImage();
}
});
JButton exit = new JButton("EXIT");
exit.setBounds(940, 750, 150, 40);
add(exit);
exit.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
imageDisplayed = new ImageIcon("C:\\Temp\\main.jpg").getImage();
}
#Override
public Dimension getPreferredSize()
{
return new Dimension(1100, 800);
}
#Override
public void paintComponent(Graphics g)
{
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
}
You've broken the paint chain...
#Override
public void paintComponent(Graphics g)
{
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
The first thing you should be calling is super.paintComponent(g)
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
The Graphics context is shared resource, meaning that the Graphics context you get has also been used to paint the other components that have also been painted. One of the jobs of paintComponent is to clear the Graphics context ready for the component to be painted (fill the background)
See Painting in AWT and Swing and Performing Custom Painting for more details
You should, also, avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. See Why is it frowned upon to use a null layout in SWING? for more details...
Before you go all defensive over getting your screen laid out just nicely, you shouldn't be doing it this way. You controls should be laid out on separate containers (using appropriate layout managers) and you "drawing surface" should be it's own component. See Laying Out Components Within a Container for more details...
If you're still not convicned, take a look at Why setting setPreferredSize() on JFrame is bad? and java expandable JDialog for examples of differences between Mac and Windows
Iam trying to position my jlabel in a jpanel what is made final because of an actionlistener.
final JPanel panelPayDetails = new JPanel();
panelPayDetails.setBounds(250, 25, 350, 250);
JLabel lblnumber = new JLabel("Insert Number:");
lblnumber.setFont(Applicatie.FONT_12_BOLD);
lblnumber.setBounds(5, 5, 200, 20);
panelPayDetails.add(lblnumber);
panelPayDetails.setVisible(false);
jpExtraDetails.add(panelPayDetails);
bill.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(bill.getSelectedItem() == "CREDIT CARD")
{
panelPayDetails.setVisible(true);
}
}
});
im not luck so far.. because the label is positioned in the middle of the jpanel.. how come?
Because the default layout manager for JPanel is FlowLayout, which centers things. Read this tutorial.