Can I initialize multiple objects in a single loop?
Here's what a snippet of my code looks like. As you can see, it becomes hard to look at from a glance and takes up too much space.
I'd like to be able to create the buttons in one for loop and then modify in another for loop.
public class MyFrame extends JFrame {
MyFrame() {
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();
JButton button8 = new JButton();
JButton button9 = new JButton();
JButton button0 = new JButton();
button1.setBounds(60, 60, 50, 50);
button2.setBounds(120,60,50,50);
button3.setBounds(180,60,50,50);
button4.setBounds(60,120,50,50);
button5.setBounds(120,120,50,50);
button6.setBounds(180,120,50,50);
button7.setBounds(60,180,50,50);
button8.setBounds(120,180,50,50);
button9.setBounds(180,180,50,50);
button0.setBounds(120,240,50,50);
}
}
Yes, you can use a for loop to create the buttons.
Here's a GUI I created.
It's not a good idea to use absolute positioning (setBounds) when creating a GUI. You should use the Swing layout managers.
I used a GridLayout to position the buttons.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TenButtonGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new TenButtonGUI());
}
#Override
public void run() {
JFrame frame = new JFrame("Ten Button GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new GridLayout(0, 3, 5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
for (int i = 0; i < 11; i++) {
if (i == 9) {
JLabel label = new JLabel(" ");
panel.add(label);
} else {
JButton button = new JButton();
button.setPreferredSize(new Dimension(50, 50));
panel.add(button);
}
}
return panel;
}
}
You could do something like this:
List<JButton> buttons = new ArrayList<>();
int[][] bounds = {{60, 60, 50, 50}, {120,60,50,50}}; //add more bound quadruplets
// iterating over the values in the bounds array
Arrays.asList(bounds).forEach(v -> {
JButton button = new JButton();
button.setBounds(v[0], v[1], v[2], v[3]);
buttons.add(button);
});
In the end you will find your buttons in the buttons List.
Related
My only problem is that I don't know how to put my border outside the panel. I've tried compound and some other border code but so far I am unable to put it outside. It either disappears or it doesn't change at all.
The output that I want:
The output that I'm getting :
I'm a newbie at swing and I have been searching and found out about compound border but I am still not so familiar on how it works with adding the buttons into the panel. (I've removed the code of me trying to do compound border because it was just a mess full of errors)
THE CODE :
package activityswing;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class swing {
public static void main (String args []) {
JFrame pa= new JFrame("Swing Activity");
//boarder
pa.setLayout(new BorderLayout());
//panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//buttons
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
JButton button6 = new JButton("Button 6");
//label
JLabel lb1= new JLabel("Panel 1");
JLabel lb2= new JLabel("Panel 2");
//adding label to panel
lb1.add(panel1);
lb1.add(panel2);
//panel1
Border line1 = BorderFactory.createLineBorder(Color.blue);
panel1.setBorder(line1);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.setBorder(BorderFactory.createTitledBorder("Panel 1"));
panel1.setBackground(Color.BLUE);
panel1.setPreferredSize(new Dimension(270,40));
//panel2
Font font2 = new Font("Verdana",Font.ITALIC,12);
Border line2 = BorderFactory.createLineBorder(Color.blue);
lb2.setFont(font2);
panel2.setBorder(line2);
LayoutManager layout = new FlowLayout();
panel2.setLayout(layout);
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel2.add(lb2);
panel2.setBorder(BorderFactory.createTitledBorder("Panel 2"));
panel2.setBackground(Color.GREEN);
panel2.setPreferredSize(new Dimension(270,40));
//FRAME
FlowLayout flow = new FlowLayout(FlowLayout.CENTER, 50,50);
pa.setLayout(flow);
pa.pack();
pa.add(panel1);
pa.add(panel2);
pa.setSize(700,200);
pa.setResizable(false);
pa.setVisible(true);
pa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pa.setLocationRelativeTo(null);
}
}
Don't add component to labels
lb1.add(panel1);
lb1.add(panel2);
Labels calculate their preferred size based the label properties (text/icon/font/etc) and not the child components.
Don't mess with the preferredSize of the components unless you are absolutely certain you willing to take over all the responsibility to calculate the components size.
The simple solution would be to create a instance of TitledBorder yourself, for example...
Font font2 = new Font("Verdana", Font.ITALIC, 12);
TitledBorder border = new TitledBorder("Panel 1");
border.setTitleFont(font2);
I'd kind of tempted to create a factory method to make this easier, but you get the general idea
To get the border "separated" from the component, I would use a compound component approach, where the outer panel contains the title and then add the inner panel into it.
If needed, you could use CompoundBorder and use a EmptyBorder on the side of the outer panel
Have a look at:
JavaDocs for TitledBorder
How to Use Borders
... for more details
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class Test {
public static void main(String args[]) {
JFrame pa = new JFrame("Swing Activity");
//boarder
pa.setLayout(new BorderLayout());
//panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//buttons
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
JButton button6 = new JButton("Button 6");
Font font2 = new Font("Verdana", Font.ITALIC, 12);
TitledBorder border1 = new TitledBorder("Panel 1");
border1.setTitleFont(font2);
JPanel outter1 = new JPanel(new BorderLayout());
outter1.setBorder(border1);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.setBackground(Color.BLUE);
outter1.add(panel1);
//panel2
LayoutManager layout = new FlowLayout();
TitledBorder border2 = new TitledBorder("Panel 2");
JPanel outter2 = new JPanel(new BorderLayout());
outter2.setBorder(border2);
panel2.setLayout(layout);
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel2.setBackground(Color.GREEN);
outter2.add(panel2);
//FRAME
FlowLayout flow = new FlowLayout(FlowLayout.CENTER, 50, 50);
pa.setLayout(flow);
pa.add(outter1);
pa.add(outter2);
pa.pack();
pa.setLocationRelativeTo(null);
pa.setVisible(true);
pa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pa.setLocationRelativeTo(null);
}
}
You can nest JPanels. That is how I implemented the output you want.
The below code is not a correction of your code, it is an example of how to achieve the output you want. Since the two JPanels in your code are quite similar, the below code only displays a single JPanel. I assume you can make the appropriate changes.
package activityswing;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Swing2 {
private JPanel createInner() {
JPanel inner = new JPanel();
inner.setBackground(Color.GREEN);
JButton button1 = new JButton("Button 1");
inner.add(button1);
JButton button2 = new JButton("Button 2");
inner.add(button2);
JButton button3 = new JButton("Button 3");
inner.add(button3);
return inner;
}
private JPanel createOuter() {
JPanel outer = new JPanel();
TitledBorder titledBorder = new TitledBorder(new LineBorder(Color.blue), "Panel 1");
Font font2 = new Font("Verdana", Font.BOLD + Font.ITALIC, 12);
titledBorder.setTitleFont(font2);
EmptyBorder emptyBorder = new EmptyBorder(10, 10, 10, 10);
CompoundBorder compoundBorder = new CompoundBorder(titledBorder, emptyBorder);
outer.setBorder(compoundBorder);
outer.add(createInner());
return outer;
}
private void showGui() {
JFrame frame = new JFrame("Swing2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createOuter());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new Swing2().showGui());
}
}
Running the above code displays the following window.
I want to make a keyboard. If I press the button it has to come up at 2.(pic). I think it's a similar way to make a calculator. Can I have some advice?
Actually I don't even know if this right. Am I OK making a JButton like that?
This is my code.
package assignment;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Random extends JFrame {
Random() {
setTitle("보안 키보드");
setLayout(new BorderLayout(10, 10));
showNorth();
showCenter();
showSouth();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(450,500);// 창크기를 정한다
setVisible(true);}
void showNorth() {
JTextField area1 = new JTextField();
JTextField area2 = new JTextField();
JPanel panel = new JPanel(new GridLayout(2, 0));
}
area2.setText("보안문자를 입력하세요.");
area1.setHorizontalAlignment(JTextField.CENTER);
area2.setHorizontalAlignment(JTextField.CENTER);
area1.setEditable(false);
area2.setEditable(false);
panel.add(area1);
panel.add(area2);
add(panel, BorderLayout.NORTH);
}
void showCenter() {
JPanel p3 = new JPanel(new GridLayout(4, 4));
p3.setLayout(new GridLayout(5, 5, 5, 5));
// 버튼 생성하기
JButton ba = new JButton("");
JButton bb = new JButton("");
JButton bc = new JButton("");
JButton bd = new JButton("");
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton er1 = new JButton("하나\n지움");
JButton erall = new JButton("전체\n지움");
p3.add(ba);// 버튼을 패널에 부착시킨다
p3.add(bb);
p3.add(bc);
p3.add(bd);
p3.add(b0);
p3.add(b1);
p3.add(b2);
p3.add(b3);
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(er1);
p3.add(erall);
add(p3, BorderLayout.CENTER); // 패널을 프레임의 중앙에 추가한다.
}
void showSouth() {
JPanel p4 = new JPanel();
JButton complete = new JButton("입력완료"); // 입력완료 버튼 생성
p4.add(complete);
p4.setLayout((LayoutManager) new FlowLayout(FlowLayout.TRAILING));
add(p4, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Random();
}
}
First of all, the code needs extreme refactor. But as you the OOP beginner, lets dive into your problem. Your class called Random needs to implement ActionListener interface and override actionPerformed method:
#Override
public void actionPerformed(ActionEvent e) {
}
Then, what you want to do is to add action listeners and set action commands for the buttons, for instance:
button.addActionListener(this);
button.addActionCommand("button")
Now you can know which button was clicked and represent something on your JTextField:
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("button")){
textField.setText("button was clicked");
}
}
This may come off as an odd question, but I would like to make a program that makes a new Jbutton instance in an existing group every time a button(called new) is pressed. It will be added under the previously added button, all buttons must be part of a group(so when one button is pressed it will deselect previously selected button if the button in the group is pressed) and should be able to make an infinite number of buttons given n number of clicks. Here is what I have so far, but honestly I don't even know how to approach this one.
public static void makebuttonpane() {
buttonpane.setLayout(new GridBagLayout());
GridBagConstraints d = new GridBagConstraints();
nbutton = new JButton("New");
d.insets = new Insets(10, 10, 0, 10);
d.anchor = GridBagConstraints.PAGE_START;
buttonpane.add(nbutton,d);
nbutton.addActionListener(new ButtonMaker());
//d.anchor = GridBagConstraints.CENTER;
}
public static void addbutton(JButton button) {
System.out.println("button made");
buttonpane.removeAll();
nbutton = new JButton("New");
d.insets = new Insets(10, 10, 0, 10);
d.anchor = GridBagConstraints.PAGE_START;
buttonpane.add(nbutton,d);
nbutton.addActionListener(new ButtonMaker());
d.gridx=0;
System.out.println(ButtonMaker.getNumb());
d.gridy= ButtonMaker.getNumb();
buttonpane.add(button,d);
frame.setVisible(true);
buttonpane.validate();
}
public static void makebuttonpane() {
buttonpane.setLayout(new GridBagLayout());
GridBagConstraints d = new GridBagConstraints();
nbutton = new JButton("New");
d.insets = new Insets(10, 10, 0, 10);
d.anchor = GridBagConstraints.PAGE_START;
buttonpane.add(nbutton,d);
nbutton.addActionListener(new ButtonMaker());
//d.anchor = GridBagConstraints.CENTER;
}
public static void addbutton(JButton button) {
System.out.println("button made");
buttonpane.removeAll();
nbutton = new JButton("New");
d.insets = new Insets(10, 10, 0, 10);
d.anchor = GridBagConstraints.PAGE_START;
buttonpane.add(nbutton,d);
nbutton.addActionListener(new ButtonMaker());
d.gridx=0;
System.out.println(ButtonMaker.getNumb());
d.gridy= ButtonMaker.getNumb();
buttonpane.add(button,d);
frame.setVisible(true);
buttonpane.validate();
}
class ButtonMaker implements ActionListener{
public static int i=1;
public void actionPerformed(ActionEvent e) {
//System.out.println("I hear you");
//System.out.println(i);
JButton button = new JButton("Button "+i);
MultiListener.addbutton(button);
i++;
}
public static int getNumb() {
return i;
}
}
It adds the first button instance but pressing 'New' only changes that first created button instead of making a new one underneath
I created a GUI that adds a JButton when you click on the "Add Button" button.
Feel free to modify this any way you want.
Mainly, I separated the construction of the JFrame, the construction of the JPanel, and the action listener. Focusing on one part at a time, I was able to code and test this GUI quickly.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class JButtonCreator implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JButtonCreator());
}
private static int buttonCount = 0;
private JButtonPanel buttonPanel;
#Override
public void run() {
JFrame frame = new JFrame("JButton Creator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonPanel = new JButtonPanel();
JScrollPane scrollPane = new JScrollPane(
buttonPanel.getPanel());
frame.add(scrollPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class JButtonPanel {
private ButtonGroup buttonGroup;
private JPanel buttonPanel;
private JPanel panel;
public JButtonPanel() {
createPartControl();
}
private void createPartControl() {
panel = new JPanel();
panel.setPreferredSize(new Dimension(640, 480));
panel.setLayout(new BorderLayout());
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(0, 5));
buttonGroup = new ButtonGroup();
panel.add(buttonPanel, BorderLayout.CENTER);
JButton newButton = new JButton("Add Button");
newButton.addActionListener(new ButtonListener(this));
panel.add(newButton, BorderLayout.AFTER_LAST_LINE);
}
public void addJButton() {
String text = "Button " + ++buttonCount;
JButton button = new JButton(text);
buttonPanel.add(button);
buttonGroup.add(button);
}
public JPanel getPanel() {
return panel;
}
}
public class ButtonListener implements ActionListener {
private JButtonPanel buttonPanel;
public ButtonListener(JButtonPanel buttonPanel) {
this.buttonPanel = buttonPanel;
}
#Override
public void actionPerformed(ActionEvent event) {
buttonPanel.addJButton();
buttonPanel.getPanel().revalidate();;
}
}
}
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();
}
}
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());
}
}