我知道如何设置边界,所以最后一个新的setbounds()调用会给出新的边界,但我不知道新的边界应该是多长/宽,它取决于输入的按钮数量,比如这里,例如:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.EmptyBorder;
public class Book_GUI extends JFrame {
private EconomyClass eco;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Book_GUI frame = new Book_GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Book_GUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//this.add(contentPane);
JButton btnBookFlight;
//eco = new EconomyClass();
//eco.setSeats(5);
for(int i=0;i<45;i++){
btnBookFlight = new JButton("Book" +i);
btnBookFlight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JButton button = (JButton)arg0.getSource();;
button.setBackground(Color.RED);
button.setOpaque(true);
}
});
btnBookFlight.setBounds(77, 351, 100, 23);
contentPane.add(btnBookFlight);
}
}
}
正如你所看到的最后5个按钮是不可见的,你必须用鼠标放大一点GUI…前10个按钮比其他按钮小,因为在9之后数字会增加,这是合乎逻辑的,但我能把它们都排成相同的顺序和大小吗?另一个问题,按钮名称"Book"只是为了测试,它应该是1A窗口,1B中间,1C通道一些空间1D通道,1E中间,1F中间,1G通道一些空间1H通道,1I中间,1J窗口和这些2A窗口以下……就像在飞机上一样,任何提示我如何安排命名和它们之间的必要空间都是非常感谢的!
您应该避免使用空布局或绝对定位来安排摆动组件。在这种情况下,总是使用最合适的布局管理器,因为它有很多优点。处理当前情况的最佳布局是GridLayout
这是使用GridLayout
修改后的代码版本
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.EmptyBorder;
public class Book_GUI extends JFrame {
// private EconomyClass eco;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Book_GUI frame = new Book_GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Book_GUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new GridLayout(9, 5));
// contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
// this.add(contentPane);
JButton btnBookFlight;
// eco = new EconomyClass();
// eco.setSeats(5);
for (int i = 0; i < 45; i++) {
btnBookFlight = new JButton("Book" + i);
btnBookFlight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JButton button = (JButton) arg0.getSource();
;
button.setBackground(Color.RED);
button.setOpaque(true);
}
});
// btnBookFlight.setBounds(77, 351, 100, 23);
contentPane.add(btnBookFlight);
}
pack();
}
}
进一步阅读:布局管理器视觉指南
为JButton集合动态分配名称,您可以使用:
List<JButton> listOfButtons = new ArrayList<JButton>(collection.size());
for (int i=0; i < collection.size(); i++) {
JButton button = new JButton();
listOfButtons.add(button);
}