Fluctuation in jbuttons - java

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Library {
JFrame frame = new JFrame("Library Management - MENU");
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
JButton button7 = new JButton();
/**
*/
public Library()
{
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("MENU");
panel.add(label);
button1.setText("ISSUE a BOOK");
button1.setBounds(100,100,200,30);
panel.add(button1);
button2.setText("RETURN a BOOK");
button2.setBounds(200,200,200,30);
panel.add(button2);
button3.setText("UPDATE/SEARCH RECORD");
button3.setBounds(300,300,200,30);
panel.add(button3);
frame.add(panel);
button1.addActionListener((ActionEvent e) -> {
frame.setTitle("ISSUE");
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
button6.setText("ISSUE a BOOK on CARD1");
button6.setBounds(100,100,200,30);
panel1.add(button6);
button7.setText("ISSUE a BOOK on CARD2");
button7.setBounds(100,100,200,30);
panel1.add(button7);
frame.add(panel1);
frame.setVisible(true);
});
button2.addActionListener((ActionEvent e) -> {
frame.setTitle("RETRUN");
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
button4.setText("RETURN a BOOK on CARD1");
button4.setBounds(100,100,200,30);
panel1.add(button4);
button5.setText("RETURN a BOOK on CARD2");
button5.setBounds(100,100,200,30);
panel1.add(button5);
frame.add(panel1);
frame.setVisible(true);
});
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
Library obj=new Library();
}
}
I am creating a library management app and i have created multiple jpanels in a frame but when i move from
panel to another it fluctuates and a previously used buttons overlap
current buttons. And even buttons are not moving at proper postions even after changing the setBounds parameters.

Try using CardLayout, here is your code with the card layout being used... Note: You're missing some actions on your buttons to return to the MAIN screen, I'll leave that to you! :-)
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Library {
JFrame frame = new JFrame("Library Management - MENU");
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
JButton button7 = new JButton();
public Library() {
JPanel cards = new JPanel(new CardLayout());
JPanel firstPanel = new JPanel();
JPanel secondPanel = new JPanel();
JPanel thirdPanel = new JPanel();
//Init some components...
JLabel label = new JLabel("MENU");
button1.setText("ISSUE a BOOK");
button2.setText("RETURN a BOOK");
button3.setText("UPDATE/SEARCH RECORD");
button4.setText("RETURN a BOOK on CARD1");
button5.setText("RETURN a BOOK on CARD2");
button6.setText("ISSUE a BOOK on CARD1");
button7.setText("ISSUE a BOOK on CARD2");
//First panel setup
firstPanel.setLayout(new FlowLayout());
firstPanel.add(label);
firstPanel.add(button1);
firstPanel.add(button2);
firstPanel.add(button3);
//Second panel setup
secondPanel.setLayout(new FlowLayout());
secondPanel.add(button6);
secondPanel.add(button7);
//Third panel setup
thirdPanel.setLayout(new FlowLayout());
thirdPanel.add(button4);
thirdPanel.add(button5);
//Show ISSUE on click of button1
button1.addActionListener((ActionEvent e) -> {
//Change cards to ISSUE panel
frame.setTitle("ISSUE");
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "ISSUE");
});
//Show RETURN on click of button2
button2.addActionListener((ActionEvent e) -> {
frame.setTitle("RETRUN");
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "RETRUN");
});
//Add content to cardlayout JPanel
cards.add(firstPanel, "MENU");
cards.add(secondPanel, "ISSUE");
cards.add(thirdPanel, "RETURN");
frame.add(cards);
//Initial card to show...
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "MENU");
//Frame constraints
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
Library obj = new Library();
}
}

Related

Java JButton Can't open new form

I am a new to Java, I met a problem. Once I click the Button, it never shows me another form, just disappear. You can see, I set the button ActionListener, but it only run the second line (close the current form).
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class login implements ActionListener {
public login(){
JFrame frame = new JFrame("Login");
frame.setLayout(new GridLayout(5,1));
JLabel label1 = new JLabel("User Name:");
JPanel panel1 = new JPanel();
frame.add(new JPanel());
frame.add(panel1);
panel1.add(label1);
JLabel label2 = new JLabel("Password:");
JPanel panel2 = new JPanel();
frame.add(panel2);
panel2.add(label2);
JPanel panel3 = new JPanel();
JButton button1 = new JButton("Register");
//button1.addActionListener(this);
JButton button2 = new JButton("Login");
//button2.addActionListener(this);
JButton button3 = new JButton("Cancel");
//button3.addActionListener(this);
panel3.add(button1);
panel3.add(button2);
panel3.add(button3);
frame.add(panel3);
JTextField JTF = new JTextField(12);
JPasswordField JPF = new JPasswordField(12);
panel1.add(JTF);
panel2.add(JPF);
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(300, 200);
frame.setLocation(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new register();
frame.dispose();
}
});
}
public static void main(String[] args){
new login();
}
public void actionPerformed(ActionEvent e ){
}
}

JButton and back button

I've this Actionlistener that change my previous panel into a second one, now I want to put a "Back" button in panel2 that take me back to panel1 maintaining its graphic setting.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args){
final JFrame Main = new JFrame("TEST");
Main.setVisible(true);
Main.setSize(600, 600);
Main.setLocationRelativeTo(null);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Adding JPanel
JPanel panel = new JPanel();
Main.add(panel);
//JPanel settings
panel.setLayout(null);
panel.setBackground(Color.GREEN);
//Adding JButton
JButton button = new JButton("Button 1");
JButton button2 = new JButton("Button2");
panel.add(button);
panel.add(button2);
//JButton settings
button.setBounds(70, 160, 200, 200);
button2.setBounds(320, 160, 200, 200);
//Button action
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JPanel panel2 = new JPanel();
//Panel2 settings
JButton button3 = new JButton("Back");
panel2.add(button3);
panel2.setBackground(Color.RED);
Main.getContentPane().removeAll();
Main.getContentPane().add(panel2);
Main.getContentPane().validate();
}
});
//Button action
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JPanel panel3 = new JPanel();
//Panel2 settings
JButton button3 = new JButton("Back");
panel3.add(button3);
panel3.setBackground(Color.YELLOW);
//Button action
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel();
Main.getContentPane().removeAll();
Main.getContentPane().add(panel);
Main.getContentPane().validate();
}
});
Main.getContentPane().removeAll();
Main.getContentPane().add(panel3);
Main.getContentPane().validate();
}
});
}
}

Adding a JPanel to an ActionListener

I have created a JPanel that has all the JRadioButtons on it that I need (it is called PortSettings). I also have a button, called port settings, when the user clicks the button, I need the JPanel to come up and display the radio buttons. I have tried to add the JPanel to the actionlistener but it doesn't work. My code is below. I have deleted all other ActionListener's from the other buttons except for the portsettings buttons. If this question is confusing I'm sorry. It's really hard to explain what I need to do. I have uploaded a drawing of what the panel will look like as well as a screenshot of my program.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JTextArea text = new JTextArea();
JLabel logLabel = new JLabel("Input/Output Log");
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
JRadioButton ethernet = new JRadioButton("Ethernet");
JRadioButton rs = new JRadioButton("RS232");
JRadioButton usbcdc = new JRadioButton("USB_CDC");
JRadioButton usbhid = new JRadioButton("USB_HID");
JButton next = new JButton("Next");
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");
JPanel PortSettings = new JPanel();
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(cancel);
JButton initialize = new JButton("Initialize");
JButton connect = new JButton("Connect");
JButton disconnect = new JButton("Disconnect");
JButton shutdown = new JButton("Shut Down");
JButton portsettings = new JButton("Port Settings");
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
JButton online = new JButton("Go Online");
JButton offline = new JButton("Go Offline");
JButton status = new JButton("Status");
JButton reboot = new JButton("Reboot");
JButton account = new JButton("Account");
JButton amount = new JButton("Amount");
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
JButton test = new JButton("Test Button #1");
JButton testing = new JButton("Test Button #2");
JRadioButton button = new JRadioButton("Radio Button");
JRadioButton button2 = new JRadioButton("Radio Button");
JCheckBox checkbox = new JCheckBox("Check Box");
JCheckBox checkbox2 = new JCheckBox("Check Box");
JPanel testPanel = new JPanel();
testPanel.add(button);
testPanel.add(button2);
testPanel.add(checkbox2);
JPanel posPanel = new JPanel();
posPanel.add(test);
posPanel.add(testing);
posPanel.add(checkbox);
JPanel llpPanel = new JPanel();
llpPanel.add(online);
llpPanel.add(offline);
llpPanel.add(status);
llpPanel.add(reboot);
llpPanel.add(account);
llpPanel.add(amount);
llpPanel.add(reset);
llpPanel.add(approvordecl);
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.add(logLabel);
frame.add(logLabel);
JPanel buttonPanel = new JPanel();
buttonPanel.add(initialize);
buttonPanel.add(connect);
buttonPanel.add(disconnect);
buttonPanel.add(shutdown);
buttonPanel.add(portsettings);
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
tabbedPane.addTab("Test", null, testPanel, "Test");
JPanel tabsPanel = new JPanel(new BorderLayout());
tabsPanel.add(tabbedPane);
frame.add(tabsPanel, BorderLayout.CENTER);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
I have tried to add a JFrame to the ActionListener then add the JPanel to the JFrame but nothing happens when I click the Port Settings button. Also, when I tried to add the JPanel to the JFrame it told me to put final in front of JPanel PortSettings = new JPanel();. Here is the code.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JTextArea text = new JTextArea();
JLabel logLabel = new JLabel("Input/Output Log");
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
JRadioButton ethernet = new JRadioButton("Ethernet");
JRadioButton rs = new JRadioButton("RS232");
JRadioButton usbcdc = new JRadioButton("USB_CDC");
JRadioButton usbhid = new JRadioButton("USB_HID");
JButton next = new JButton("Next");
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");
final JPanel PortSettings = new JPanel();
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(cancel);
JPanel accountButton = new JPanel();
accountButton.add(ok);
accountButton.add(cancel);
JPanel apprvordecl = new JPanel();
apprvordecl.add(apprve);
apprvordecl.add(decline);
JPanel amountButton = new JPanel();
amountButton.add(ok);
amountButton.add(cancel);
JButton initialize = new JButton("Initialize");
JButton connect = new JButton("Connect");
JButton disconnect = new JButton("Disconnect");
JButton shutdown = new JButton("Shut Down");
JButton portsettings = new JButton("Port Settings");
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame port = new JFrame("Port Settings");
port.add(PortSettings);
frame.setVisible(true);
}
});
JButton online = new JButton("Go Online");
JButton offline = new JButton("Go Offline");
JButton status = new JButton("Status");
JButton reboot = new JButton("Reboot");
JButton account = new JButton("Account");
JButton amount = new JButton("Amount");
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
JButton test = new JButton("Test Button #1");
JButton testing = new JButton("Test Button #2");
JRadioButton button = new JRadioButton("Radio Button");
JRadioButton button2 = new JRadioButton("Radio Button");
JCheckBox checkbox = new JCheckBox("Check Box");
JCheckBox checkbox2 = new JCheckBox("Check Box");
JPanel testPanel = new JPanel();
testPanel.add(button);
testPanel.add(button2);
testPanel.add(checkbox2);
JPanel posPanel = new JPanel();
posPanel.add(test);
posPanel.add(testing);
posPanel.add(checkbox);
JPanel llpPanel = new JPanel();
llpPanel.add(online);
llpPanel.add(offline);
llpPanel.add(status);
llpPanel.add(reboot);
llpPanel.add(account);
llpPanel.add(amount);
llpPanel.add(reset);
llpPanel.add(approvordecl);
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.add(logLabel);
frame.add(logLabel);
JPanel buttonPanel = new JPanel();
buttonPanel.add(initialize);
buttonPanel.add(connect);
buttonPanel.add(disconnect);
buttonPanel.add(shutdown);
buttonPanel.add(portsettings);
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
tabbedPane.addTab("Test", null, testPanel, "Test");
JPanel tabsPanel = new JPanel(new BorderLayout());
tabsPanel.add(tabbedPane);
frame.add(tabsPanel, BorderLayout.CENTER);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
You´re on the right track, but you do not want to add your PortSettings panel to a new JFrame but somewhere on your previously built one, assigned to the local variable frame. So your action listener should rather be
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.add(PortSettings, BorderLayout.SOUTH);
frame.pack();
}
});
(This is assuming that you actually want to add it to the frame at that instant and not add it invisibly right from the start and turn it visible, like #Aleksei suggested.)
The error message about final is because you use PortSettings in an (anonymous) inner class - viz., the ActionListener. In my proposed modification the same goes for frame, so you need to adapt its declaration as well:
final JFrame frame = new JFrame();
The reason why is quite technical and beside the point right now: just do it.
If instead you want the panel to appear in a separate window, you need a JDialog for that, not a second JFrame:
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(frame);
dialog.add(PortSettings);
dialog.pack();
dialog.setVisible(true);
}
});
Take a look at the JOptionPane class for a rich choice of ways to get more functionality out of dialogs.
Just add the action listener to all your buttons.
like this:
yourButton.addActionListener(this);
Do that for all the buttons.
Then take your TestPalication class's actionPreformed method and do whatever:
#Override
public void actionPerformed(ActionEvent arg0) {
((JRadioButton) arg0.getSource()).setTitle("Clicked!");
}
Your question was a little bit confusing but I hope this clarifies a little bit.

Java: vertical alignment within JPanel

I am trying to vertically align (center) both JLabels inside one JPanel.
JPanel panel = new JPanel();
panel.setPreferredSize(size);
JLabel label1 = new JLabel(icon);
JLabel label2 = new JLabel("text");
panel.add(label1);
panel.add(label2);
I have tried using setAligmentY() with no success. Both labels always appear on the top of JPanel.
UPD: Labels should be located next to each other like using FlowLayout, but in the middle of the JPanel.
Use a GridBagLayout with the default constraints. Here is a small demo code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestVerticalAlignement {
protected void initUI() {
final JFrame frame = new JFrame();
frame.setTitle("Test vertical alignement");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel label1 = new JLabel("label1");
JLabel label2 = new JLabel("label2");
panel.add(label1, gbc);
panel.add(label2, gbc);
frame.add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestVerticalAlignement().initUI();
}
});
}
}
you can see this answer:
https://stackoverflow.com/a/18073909/189411
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
Use gridlayout, simple.
That should work.
Consider my following example:
import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
/*
<applet code=AJ07 width=450 height=450>
</applet>
*/
public class AJ07 extends JApplet{
Container c=null;
public void init(){
JPanel pTop=new JPanel();
JPanel pLeft=new JPanel();
JPanel pCenter=new JPanel();
JPanel pProperties=new JPanel();
pLeft.setLayout(new GridLayout(20,1));
c=this.getContentPane();
JButton bNew=new JButton("New");
pTop.add(bNew);
JButton bOpen=new JButton("Open");
pTop.add(bOpen);
JButton bSave=new JButton("Save");
pTop.add(bSave);
JButton bSaveAll=new JButton("Save All");
pTop.add(bSaveAll);
JButton bRun=new JButton("Run");
pTop.add(bRun);
JButton bStop=new JButton("Stop");
pTop.add(bStop);
JButton bPause=new JButton("Pause");
pTop.add(bPause);
JButton bText=new JButton("TextBox");
pLeft.add(bText);
JButton bButton=new JButton("Button");
pLeft.add(bButton);
pProperties.setLayout(new GridLayout(20,1));
pProperties.add(new Label("BackColor"));
pProperties.add(new Label("ForeColor"));
c.add(new TextArea(),BorderLayout.CENTER);
c.add(pTop,BorderLayout.NORTH);
c.add(pLeft,BorderLayout.WEST);
c.add(new Label("Project Loaded Successfully!"),BorderLayout.SOUTH);
c.add(pProperties,BorderLayout.EAST);
//c.add(pCenter,BorderLayout.CENTER);
}
}
for which the output is as follows:

JButton Layout Issue

I'm putting together the basic layout for a contacts book, and I want to know how I can make the 3 test buttons span from edge to edge just as the arrow buttons do.
private static class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Code Placeholder");
}
}
public static void main(String[] args) {
//down button
ImageIcon downArrow = new ImageIcon("down.png");
JButton downButton = new JButton(downArrow);
ButtonHandler downListener = new ButtonHandler();
downButton.addActionListener(downListener);
//up button
ImageIcon upArrow = new ImageIcon("up.png");
JButton upButton = new JButton(upArrow);
ButtonHandler upListener = new ButtonHandler();
upButton.addActionListener(upListener);
//contacts
JButton test1Button = new JButton("Code Placeholder");
JButton test2Button = new JButton("Code Placeholder");
JButton test3Button = new JButton("Code Placeholder");
Box box = Box.createVerticalBox();
box.add(test1Button);
box.add(test2Button);
box.add(test3Button);
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(box, BorderLayout.CENTER);
content.add(downButton, BorderLayout.SOUTH);
content.add(upButton, BorderLayout.NORTH);
JFrame window = new JFrame("Contacts");
window.setContentPane(content);
window.setSize(400, 600);
window.setLocation(100, 100);
window.setVisible(true);
}
Following up on #kloffy's suggestion:
package playground.tests;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import junit.framework.TestCase;
public class ButtonTest extends TestCase {
public void testThreeButtons() throws Exception {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout());
JButton button1 = new JButton("A");
JButton button2 = new JButton("B");
JButton button3 = new JButton("C");
panel.add(button1);
panel.add(button2);
panel.add(button3);
JFrame window = new JFrame("Contacts");
window.setContentPane(panel);
window.setSize(300, 600);
window.pack();
window.setVisible(true);
int width = button1.getWidth();
assertEquals(width, button2.getWidth());
assertEquals(width, button3.getWidth());
}
}

Categories