我正在做一个简单的图形用户界面,徽标在第一行,然后其他的东西在下一行。问题是徽标太小了,所以JComboBox和JTextArea也在那一行,我怎么能防止这种情况,只在第一行做徽标呢?谢谢!
public class TimerMenu {
private JFrame frame;
private JLabel background, logo;
private JTextArea timeText;
private JButton startTimerButton;
private JComboBox timeUnitChoice;
public TimerMenu(){
frame = new JFrame("Timer");
startTimerButton = new JButton("Start Timer");
startTimerButton.setPreferredSize(new Dimension(135, 30));
startTimerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO: CHANGE TO SOMETHING NICER
JOptionPane.showMessageDialog(null, "This feature hasn't been implemented yet.", "We're sorry!",
JOptionPane.ERROR_MESSAGE);
}
});
// Creating drop down menu.
String[] timeChoices = { "Nanoseconds", "Microseconds", "Milliseconds", "Seconds", "Minutes", "Hours", "Days"};
// Giving the choices from the array of 'timeChoices'
timeUnitChoice = new JComboBox(timeChoices);
// Setting the default option to 'Minutes' (4th choice, starting at 0 as its an array!)
timeUnitChoice.setSelectedIndex(4);
try {
background = new JLabel(new ImageIcon(ImageIO.read(getClass()
.getResourceAsStream("/me/devy/alarm/clock/resources/background.jpg"))));
logo = new JLabel(new ImageIcon(ImageIO.read(getClass()
.getResourceAsStream("/me/devy/alarm/clock/resources/timer.png"))));
} catch (IOException e) {
e.printStackTrace();
}
// Creating simple text
background.setLayout(new FlowLayout());
frame.setContentPane(background);
frame.add(logo);
frame.add(timeUnitChoice);
// Creating a text field
timeText = new JTextArea("Length:");
timeText.setEditable(false);
frame.add(timeText);
frame.add(startTimerButton);
frame.setVisible(true);
frame.setSize(550, 250);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2条答案
按热度按时间flvlnr441#
你想要的是把这个
被这种
您通常需要堆叠不同的布局,以便能够以有意义的方式排列组件。
另请参阅:swing flow layout break element
y0u0uwnf2#