按钮没有显示在正确的位置

时间:2013-06-13 01:14:26

标签: java swing button layout layout-manager

如何让按钮显示在textarea下面的行上?

//Imports
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

public class ProfessorPhysInstall {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //Variables
        final JFrame mainframe = new JFrame();
        mainframe.setSize(500, 435);
        final JPanel cards = new JPanel(new CardLayout());
        final CardLayout cl = (CardLayout)(cards.getLayout());
        mainframe.setTitle("Future Retro Gaming Launcher");
        //Screen1
        JPanel screen1 = new JPanel();
        JTextPane TextPaneScreen1 = new JTextPane();
        TextPaneScreen1.setEditable(false);
        TextPaneScreen1.setBackground(new java.awt.Color(240, 240, 240));
        TextPaneScreen1.setText("Welcome to the install wizard  for Professor Phys!\n\nPlease agree to the following terms and click the next button to continue.");
        TextPaneScreen1.setSize(358, 48);
        TextPaneScreen1.setLocation(0, 0);
        TextPaneScreen1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        TextPaneScreen1.setMargin(new Insets(4,4,4,4));
        screen1.add(TextPaneScreen1);
        JTextArea TextAreaScreen1 = new JTextArea();
        JScrollPane sbrText = new JScrollPane(TextAreaScreen1);
        TextAreaScreen1.setRows(15);
        TextAreaScreen1.setColumns(40);
        TextAreaScreen1.setEditable(false);
        TextAreaScreen1.setText("Lots of text");
        TextAreaScreen1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        TextAreaScreen1.setMargin(new Insets(4,4,4,4));
        screen1.add(sbrText);
        final JCheckBox Acceptance = new JCheckBox();
        Acceptance.setText("I Accept The EULA Agreenment.");
        screen1.add(Acceptance);
        final JButton NextScreen1 = new JButton();
        NextScreen1.setText("Next");
        NextScreen1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(Acceptance.isSelected())
                    cl.next(cards);
            }
        });      
        screen1.add(NextScreen1);        
        JButton CancelScreen1 = new JButton();
        CancelScreen1.setText("Cancel");
        CancelScreen1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });      
        screen1.add(CancelScreen1);
        cards.add(screen1);

        //Screen2
        JPanel screen2 = new JPanel();
        JTextPane TextPaneScreen2 = new JTextPane();
        TextPaneScreen2.setEditable(false);
        TextPaneScreen2.setBackground(new java.awt.Color(240, 240, 240));
        TextPaneScreen2.setText("Please select the Future Retro Gaming Launcher.");
        TextPaneScreen2.setSize(358, 48);
        TextPaneScreen2.setLocation(0, 0);
        TextPaneScreen2.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        TextPaneScreen2.setMargin(new Insets(4,4,4,4));
        screen2.add(TextPaneScreen2);
        final JButton BackScreen2 = new JButton();
        BackScreen2.setText("Back");
        BackScreen2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(Acceptance.isSelected())
                    cl.previous(cards);
            }
        });    
        screen2.add(BackScreen2);     
        final JButton NextScreen2 = new JButton();
        NextScreen2.setText("Next");
        NextScreen2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(Acceptance.isSelected())
                    cl.next(cards);
            }
        });      
        screen2.add(NextScreen2);        
        JButton CancelScreen2 = new JButton();
        CancelScreen2.setText("Cancel");
        CancelScreen2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });      
        screen2.add(CancelScreen2);
        cards.add(screen2);

        mainframe.add(cards);
        mainframe.setVisible(true);
    }
}

这就是它的样子

enter image description here

3 个答案:

答案 0 :(得分:2)

使用适当的LayoutManager

您已将所有组件添加到screen2,默认情况下为FlowLayout。有许多不同的方法可以实现您的目标。

更多阅读:Using Layout Managers

答案 1 :(得分:2)

JPanel的默认布局管理器为FlowLayout。顾名思义,它只是一个接一个地流动组件。

你需要尝试不同的布局管理器,像GridBagLayout这样的东西会做你想要的,但它是一个复杂的布局。您可能更好地尝试使用复合布局来实现相同的效果。

也就是说,将UI分解为小块,并为每个组应用不同的布局管理器。

退房......

你也会因阅读而受益......

...诗

您可能还想考虑将每个屏幕分成它自己的类,现在的代码很难阅读和理解 - 恕我直言

答案 2 :(得分:2)

通常的方法是结合所需外观的布局。 E.G。

enter image description here

import java.awt.*;
import javax.swing.*;

public class ProfessorPhysInstall {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Variables
        final JFrame mainframe = new JFrame();
        mainframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainframe.setSize(500, 435);
        final JPanel cards = new JPanel(new BorderLayout());
        mainframe.setTitle("Future Retro Gaming Launcher");

        //Screen2
        JPanel screen2 = new JPanel();
        JTextPane TextPaneScreen2 = new JTextPane();
        TextPaneScreen2.setText("Please select the Future Retro Gaming Launcher.");
        // set the preferred size, rather than the size!
        //TextPaneScreen2.setSize(358, 48);
        TextPaneScreen2.setPreferredSize(new Dimension(358, 48));
        TextPaneScreen2.setBorder(
            BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.black),
                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        TextPaneScreen2.setMargin(new Insets(4,4,4,4));
        screen2.add(TextPaneScreen2);

        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER, 5,5));
        final JButton BackScreen2 = new JButton();
        BackScreen2.setText("Back");
        buttons.add(BackScreen2);
        final JButton NextScreen2 = new JButton("Next");
        buttons.add(NextScreen2);

        JButton CancelScreen2 = new JButton("Cancel");
        buttons.add(CancelScreen2);

        cards.add(screen2, BorderLayout.PAGE_START);
        cards.add(buttons, BorderLayout.PAGE_END);

        mainframe.add(cards);
        mainframe.pack();
        mainframe.setVisible(true);
    }
}

提示

  1. 请了解课程,方法和方法的常见Java naming conventions(特别是用于名称的情况)属性名称&一贯地使用它们。
  2. 为了更好地提供帮助,请发布SSCCE
  3. 不要设置顶级容器的大小。而是布局内容&致电pack()
  4. 有关如何合并布局以创建所需布局的建议,请参阅Nested Layout Example


  5. 还听取了其他两个答案已经提出的建议。