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(u开发者_如何学编程pListener);
//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());
}
}
精彩评论