java 如何转到FlowLayout上的下一行

relj7zay  于 2022-12-28  发布在  Java
关注(0)|答案(2)|浏览(111)

我正在做一个简单的图形用户界面,徽标在第一行,然后其他的东西在下一行。问题是徽标太小了,所以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);
    }

}
flvlnr44

flvlnr441#

你想要的是把这个

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

y0u0uwnf

y0u0uwnf2#

JPanel panel3 = new JPanel(false);    
panel3.setLayout(new FlowLayout(FlowLayout.LEFT));  //using flow layout
JLabel empty_line = new JLabel("");   // <--- empty label to effect next row
empty_line.setPreferredSize(new Dimension(3000,0));
//i used 3000 for x-axis, should over flow most screens but  should not displace 
//the next components under it
panel3.add(component1); 
panel3.add(empty_line); //add empty label
panel3.add(component2); //components 1 and component2 must be declared before

相关问题