我知道如何设置边界,所以最后一个新的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窗口......就像在飞机上一样,我可以非常感谢任何提示我如何安排它们以及它们之间的必要空间!
答案 0 :(得分:5)
您应该避免使用空布局或绝对定位来安排摆动组件。在这种情况下始终使用最合适的布局管理器,因为它具有很多优点。处理当前情况的最佳布局是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();
}
}
答案 1 :(得分:1)
为动态名称分配给JButton集合,您可以使用:
List<JButton> listOfButtons = new ArrayList<JButton>(collection.size());
for (int i=0; i < collection.size(); i++) {
JButton button = new JButton();
listOfButtons.add(button);
}