-1

如何让按钮出现在文本区域下方的行上?

//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);
    }
}

这就是它的样子

在此处输入图像描述

4

3 回答 3

2

使用适当的LayoutManager

您已经添加了默认情况下screen2具有 a的所有组件。FlowLayout有许多不同的方法可以实现你所追求的。

更多阅读:使用布局管理器

于 2013-06-13T01:30:43.880 回答
2

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

您将需要尝试不同的布局管理器,比如GridBagLayout会做您想做的事情,但它是一个复杂的布局。您最好尝试使用复合布局来达到相同的效果。

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

查看...

您还将受益于阅读...

附...

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

于 2013-06-13T01:38:58.083 回答
2

通常的方法是组合布局以获得所需的外观。例如

在此处输入图像描述

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 命名约定(特别是名称的大小写)并一致地使用它们。
  2. 为了尽快获得更好的帮助,请发布SSCCE
  3. 不要设置顶级容器的大小。而是布局内容并调用pack()
  4. 有关如何组合布局以创建所需布局的想法,请参阅嵌套布局示例。


  5. 还要注意其他两个答案已经提供的建议。
于 2013-06-13T01:54:37.763 回答