JPanel 不适应任何变化

起初,我在 JFrame 中调用了我的面板。一切都很好。


现在我想从另一个类调用面板以在选择其中一个后切换到同一个 JFrame 中的不同面板。我遇到的问题如下:


JFrame 不会以任何方式响应我设置到面板的更改。例如,无论是在 Panel 类中调整它的大小还是在 JFrame 类中调整它的大小。


面板总是像 Flowlayout 一样扩展为与 JFrame 相同的大小。但是尝试更改布局也不起作用,也不应该有任何影响,因为 Panel 以前工作过(当在 JFrame 中编码而不是在单独的类中时)。


我如何以这种方式修复代码,以便我可以调整面板并使 JFrame 识别这些更改?我目前正在使用 Eclipse 和 NetBeans;在任何一个中都不起作用。


主要的:


package calculatorXtreme;


import javax.swing.JFrame;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JMenu;


public class Main {


    public static void main(String[] args) {

        Interface interface1 =new Interface();

        interface1.show();

    }


}


holdtom
浏览 191回答 1
1回答

千巷猫影

您的问题来自未将JFrame 和 JPanel的布局都设置为null,以便setBounds(...)生效。不以这种方式显式设置布局,JPanel 将盲目使用其默认 FlowLayout,而 JFrame(实际上是其 contentPane)将盲目使用其默认 BorderLayout因此需要进行这些更改:class PannelDez extends JPanel {&nbsp; &nbsp; // .....&nbsp; &nbsp; public PannelDez() {&nbsp; &nbsp; &nbsp; &nbsp; // Panel&nbsp; &nbsp; &nbsp; &nbsp; setLayout(null);&nbsp; &nbsp; &nbsp; &nbsp; // ....&nbsp; &nbsp; }&nbsp; &nbsp; // ....}class Interface extends JFrame {&nbsp; &nbsp; public Interface() {&nbsp; &nbsp; &nbsp; &nbsp; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; setLayout(null);&nbsp; &nbsp; &nbsp; &nbsp; // PanelAdden&nbsp; &nbsp; &nbsp; &nbsp; // .....&nbsp; &nbsp; }&nbsp; &nbsp; // ..........}但话虽如此,我重申我之前说过的话:虽然空布局和 setBounds() 对 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整您的组件的大小,它们是增强或维护的皇家女巫,它们放置在滚动窗格中时完全失败,在所有平台或与原始屏幕分辨率不同的屏幕上查看时,它们看起来很糟糕.同样,解决这个问题的更好方法是学习和使用布局管理器。例如,您正在创建一个计算器,这很适合使用两个 JPanel,一个用于保存使用 GridLayout 的按钮以创建按钮网格,另一个使用 BorderLayout 来保存上述 JPanel在其中心位置,并将您的 JTextField 保持在顶部位置,BorderLayout.PAGE_START位置。另请注意,最好尽量避免代码重复,例如我们在所有 ActionListener 中看到的。您只需要两个ActionListeners —— 每个数字按钮一个,每个操作一个。下面的代码创建了一个如上所述的 Swing GUI,一个带有嵌套的 JPanel,外部使用 BorderLayout,内部使用 GridLayout(0, 4),它代表可变行数(0)和 4 列。GUI的图像:和代码:导入 java.awt.BorderLayout; 导入 java.awt.Font; 导入 java.awt.GridLayout; 导入 java.awt.event.ActionEvent; 导入 java.awt.event.ActionListener; 导入 javax.swing.*;@SuppressWarnings("serial")public class FooGui02 extends JPanel {&nbsp; &nbsp; private static final Font BUTTON_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 36);&nbsp; &nbsp; private static final Font ANZEIGE_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 24);&nbsp; &nbsp; private JTextField anzeige = new JTextField(10);&nbsp; &nbsp; RechnerInt interfaceRechner = new RechnerInt();&nbsp; &nbsp; public FooGui02() {&nbsp; &nbsp; &nbsp; &nbsp; anzeige.setFont(ANZEIGE_FONT);&nbsp; &nbsp; &nbsp; &nbsp; // anzeige.setEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; anzeige.setBackground(null);&nbsp; &nbsp; &nbsp; &nbsp; anzeige.setFocusable(false); // user can't interact with this&nbsp; &nbsp; &nbsp; &nbsp; // create a grid of 4 columns and variable rows:&nbsp; &nbsp; &nbsp; &nbsp; JPanel btnPanel = new JPanel(new GridLayout(0, 4));&nbsp; &nbsp; &nbsp; &nbsp; // create first row&nbsp; &nbsp; &nbsp; &nbsp; // add 3 blank JLabels&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 3; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(new JLabel());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createOperationButton(Operation.DIVISION));&nbsp; &nbsp; &nbsp; &nbsp; // second row&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createNumberButton(7));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createNumberButton(8));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createNumberButton(9));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createOperationButton(Operation.MULTIPLICATION));&nbsp; &nbsp; &nbsp; &nbsp; // third row&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createNumberButton(4));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createNumberButton(5));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createNumberButton(6));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createOperationButton(Operation.SUBTRACTION));&nbsp; &nbsp; &nbsp; &nbsp; // forth row&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createNumberButton(1));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createNumberButton(2));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createNumberButton(3));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createOperationButton(Operation.ADDITION));&nbsp; &nbsp; &nbsp; &nbsp; // fifth row&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(new JLabel());&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createNumberButton(0));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createOperationButton(Operation.CLEAR));&nbsp; &nbsp; &nbsp; &nbsp; btnPanel.add(createOperationButton(Operation.EQUALS));&nbsp; &nbsp; &nbsp; &nbsp; setLayout(new BorderLayout());&nbsp; &nbsp; &nbsp; &nbsp; add(anzeige, BorderLayout.PAGE_START);&nbsp; &nbsp; &nbsp; &nbsp; add(btnPanel);&nbsp; &nbsp; }&nbsp; &nbsp; private JButton createOperationButton(Operation operation) {&nbsp; &nbsp; &nbsp; &nbsp; JButton button = new JButton(operation.getText());&nbsp; &nbsp; &nbsp; &nbsp; button.addActionListener(new OperationListener(operation));&nbsp; &nbsp; &nbsp; &nbsp; button.setFont(BUTTON_FONT);&nbsp; &nbsp; &nbsp; &nbsp; return button;&nbsp; &nbsp; }&nbsp; &nbsp; private JButton createNumberButton(int number) {&nbsp; &nbsp; &nbsp; &nbsp; JButton button = new JButton("" + number);&nbsp; &nbsp; &nbsp; &nbsp; button.addActionListener(new NumberListener(number));&nbsp; &nbsp; &nbsp; &nbsp; button.setFont(BUTTON_FONT);&nbsp; &nbsp; &nbsp; &nbsp; return button;&nbsp; &nbsp; }&nbsp; &nbsp; private class OperationListener implements ActionListener {&nbsp; &nbsp; &nbsp; &nbsp; private Operation operation;&nbsp; &nbsp; &nbsp; &nbsp; public OperationListener(Operation operation) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.operation = operation;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String text = anzeige.getText();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (operation) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case CLEAR:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.operators.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.numbers.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.numbersInt.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anzeige.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ADDITION:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.addNumber(Integer.valueOf(text));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.addOperators("+");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anzeige.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case SUBTRACTION:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.addNumber(Integer.valueOf(text));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.addOperators("-");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anzeige.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MULTIPLICATION:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.addNumber(Integer.valueOf(text));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.addOperators("*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anzeige.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case DIVISION:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.addNumber(Integer.valueOf(text));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.addOperators("/");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anzeige.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case EQUALS:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.addNumber(Integer.valueOf(text));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.addOperators("=");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anzeige.setText("" + interfaceRechner.berechneInt());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.operators.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.numbers.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; interfaceRechner.numbersInt.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private class NumberListener implements ActionListener {&nbsp; &nbsp; &nbsp; &nbsp; private int number;&nbsp; &nbsp; &nbsp; &nbsp; public NumberListener(int number) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.number = number;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String text = anzeige.getText();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text += number;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anzeige.setText(text);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static void createAndShowGui() {&nbsp; &nbsp; &nbsp; &nbsp; FooGui02 mainPanel = new FooGui02();&nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("Calculator");&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; frame.getContentPane().add(mainPanel);&nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; frame.setResizable(false);&nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SwingUtilities.invokeLater(() -> createAndShowGui());&nbsp; &nbsp; }}enum Operation {&nbsp; &nbsp; DIVISION("/"), MULTIPLICATION("*"), SUBTRACTION("-"),&nbsp;&nbsp; &nbsp; ADDITION("+"), CLEAR("C"), EQUALS("=");&nbsp; &nbsp; private String text;&nbsp; &nbsp; private Operation(String text) {&nbsp; &nbsp; &nbsp; &nbsp; this.text = text;&nbsp; &nbsp; }&nbsp; &nbsp; public String getText() {&nbsp; &nbsp; &nbsp; &nbsp; return text;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java