如何进入FlowLayout的下一行

时间:2015-11-26 06:29:15

标签: java swing jframe flowlayout

我正在制作一个简单的GUI,第一行是徽标,下一行是剩下的东西。问题是徽标是小的,因此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);
    }

}

1 个答案:

答案 0 :(得分:1)

你想要的是取代这个

background.setLayout(new FlowLayout());
frame.add(logo);
frame.add(timeUnitChoice);
frame.add(timeText);
frame.add(startTimerButton);

由此(种类)

background.setLayout(new FlowLayout(FlowLayout.LEFT));

JPanel logoPnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
logoPnl.add(logo);
JPanel fnctnPnl = new JPanel(new FlowLayout());
fnctnPnl.add(timeUnitChoice);
fnctnPnl.add(timeText);
fnctnPnl.add(startTimerButton);

JPanel borderPnl = new JPanel(new BorderLayout());
borderPnl.add(logoPnl, BorderLayout.NORTH);
borderPnl.add(fnctnPnl, BorderLayout.SOUTH);

JPanel container = new JPanel(new FlowLayout(FlowLayout.LEFT));
container.add(borderPnl);

frame.getContentPane().add(container);

您通常需要为您堆叠不同的布局,以便能够以有意义的方式安排组件。

另请参阅:swing flow layout break element