gpt4 book ai didi

java - JPanel不适应任何变化

转载 作者:行者123 更新时间:2023-11-30 05:58:19 25 4
gpt4 key购买 nike

首先,我在 JFrame 中调用了面板。一切都很好。

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

The JFrame does not respond in any way to changes I set to the panel. Neither when e.g. resizing it inside the Panel class nor when resizing it in the JFrame class.

面板始终像 Flowlayout 一样展开与 JFrame 相同的尺寸。但是尝试更改布局也不起作用,也不应该以任何方式产生影响,因为面板以前可以工作(当在 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();
}

}

面板:

package calculatorXtreme;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PannelDez extends JPanel {
JLabel text = new JLabel();
JButton delete = new JButton();
JButton zero = new JButton();
JButton eins = new JButton();
JButton zwei = new JButton();
JButton drei = new JButton();
JButton vier = new JButton();
JButton fuenf = new JButton();
JButton sechs = new JButton();
JButton sieben = new JButton();
JButton acht = new JButton();
JButton neun = new JButton();
JButton addieren = new JButton();
JButton subtrahieren = new JButton();
JButton multiplizieren = new JButton();
JButton dividieren = new JButton();
JButton ergebnis = new JButton();
JTextField anzeige = new JTextField();
String erg = "";
int temp1;
RechnerInt interfaceRechner = new RechnerInt();

public PannelDez() {

//Panel

this.setSize(400, 500);
anzeige.setBounds(125,100, 200, 50);
anzeige.setSize(200, 50);
anzeige.setEditable(false);
anzeige.setText(erg);
this.add(anzeige);


//Buttons

delete.setText("C");
zero.setText("0");
eins.setText("1");
zwei.setText("2");
drei.setText("3");
vier.setText("4");
fuenf.setText("5");
sechs.setText("6");
sieben.setText("7");
acht.setText("8");
neun.setText("9");
addieren.setText("+");
subtrahieren.setText("-");
multiplizieren.setText("*");
dividieren.setText("/");
ergebnis.setText("=");



this.add(delete);
this.add(zero);
this.add(eins);
this.add(zwei);
this.add(drei);
this.add(vier);
this.add(fuenf);
this.add(sechs);
this.add(sieben);
this.add(acht);
this.add(neun);
this.add(addieren);
this.add(subtrahieren);
this.add(multiplizieren);
this.add(dividieren);
this.add(ergebnis);

delete.addActionListener(new ListenDelete());
zero.addActionListener(new ListenZero());
eins.addActionListener(new ListenEins());
zwei.addActionListener(new ListenZwei());
drei.addActionListener(new ListenDrei());
vier.addActionListener(new ListenVier());
fuenf.addActionListener(new ListenFuenf());
sechs.addActionListener(new ListenSechs());
sieben.addActionListener(new ListenSieben());
acht.addActionListener(new ListenAcht());
neun.addActionListener(new ListenNeun());
addieren.addActionListener(new ListenAddieren());
subtrahieren.addActionListener(new ListenSubtrahieren());
multiplizieren.addActionListener(new ListenMultiplizieren());
dividieren.addActionListener(new ListenDividieren());
ergebnis.addActionListener(new ListenErgebnis());

/* JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout()); */

}

public void resizeButtons()
{
delete.setBounds(225, 350, 50, 50);
zero.setBounds(175, 350,50,50);
eins.setBounds(125, 300,50,50);
zwei.setBounds(175, 300,50,50);
drei.setBounds(225, 300,50,50);
vier.setBounds(125, 250,50,50);
fuenf.setBounds(175, 250,50,50);
sechs.setBounds(225, 250,50,50);
sieben.setBounds(125, 200,50,50);
acht.setBounds(175, 200,50,50);
neun.setBounds(225, 200,50,50);
addieren.setBounds(275, 300,50,50);
subtrahieren.setBounds(275, 250,50,50);
multiplizieren.setBounds(275, 200,50,50);
dividieren.setBounds(275, 150,50,50);
ergebnis.setBounds(275, 350,50,50);
}

class ListenDelete implements ActionListener {
public void actionPerformed (ActionEvent e) {
interfaceRechner.operators.clear();
interfaceRechner.numbers.clear();
interfaceRechner.numbersInt.clear();
anzeige.setText("");
}
}
class ListenZero implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "0");
erg = anzeige.getText();
}
}
class ListenEins implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "1");
erg = anzeige.getText();
return;
}
}
class ListenZwei implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "2");
erg = anzeige.getText();
}
}
class ListenDrei implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "3");
erg = anzeige.getText();
}
}
class ListenVier implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "4");
erg = anzeige.getText();
}
}
class ListenFuenf implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "5");
erg = anzeige.getText();
}
}
class ListenSechs implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "6");
erg = anzeige.getText();
}
}
class ListenSieben implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "7");
erg = anzeige.getText();
}
}
class ListenAcht implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "8");
erg = anzeige.getText();
}
}
class ListenNeun implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "9");
erg = anzeige.getText();
}
}
class ListenAddieren implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
interfaceRechner.addNumber(Integer.valueOf(erg));
interfaceRechner.addOperators("+");
anzeige.setText("");
erg = null;
}
}
class ListenSubtrahieren implements ActionListener {

public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
interfaceRechner.addNumber(Integer.valueOf(erg));
interfaceRechner.addOperators("-");
anzeige.setText("");
erg = null;
}
}
class ListenMultiplizieren implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
interfaceRechner.addNumber(Integer.valueOf(erg));
interfaceRechner.addOperators("*");
anzeige.setText("");
erg = null;
}
}
class ListenDividieren implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
interfaceRechner.addNumber(Integer.valueOf(erg));
interfaceRechner.addOperators("/");
anzeige.setText("");
erg = null;
}
}
class ListenErgebnis implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
interfaceRechner.addNumber(Integer.valueOf(erg));
interfaceRechner.addOperators("=");
anzeige.setText(""+interfaceRechner.berechneInt());

}
}

}

JFrame:

package calculatorXtreme;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.plaf.ColorUIResource;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Dimension;


public class Interface extends JFrame {

public Interface(){

// PanelAdden

PannelDez panel2 = new PannelDez();
//panel2.setSize(400, 500);
this.setSize(700, 800);
panel2.setSize(300, 200);
this.setVisible(true);
this.setResizable(false);
panel2.setSize(400, 500);
panel2.setBounds(0, 0, 400, 500);
panel2.resizeButtons();
this.add(panel2);

// Menüleiste

JMenuBar jmb = new JMenuBar();
this.setJMenuBar(jmb);
JMenu file = new JMenu("Option");
jmb.add(file);
JMenuItem dez = new JMenuItem("Dezimal-Rechner");
file.add(dez);
JMenuItem hex = new JMenuItem("Hex-Rechner");
file.add(hex);
this.setVisible(true);


}

}

RechnerInt:

package calculatorXtreme;

import java.util.ArrayList;
import java.util.List;

public class RechnerInt {
List<String> numbers = new ArrayList<>();
List<String> operators = new ArrayList<>();
List<Double> numbersDouble = new ArrayList<>();
List<Integer> numbersInt = new ArrayList<>();
double zahl1, zahl2, ergebnis;
String op;
boolean abbruch = true;

public double rechnen(String op, double zahl1, double zahl2)

{

switch (op) {
case "+":
ergebnis = zahl1 + zahl2;
return ergebnis;
case "-":
ergebnis = zahl1 - zahl2;
return ergebnis;
case "/":
ergebnis = zahl1 / zahl2;
return ergebnis;
case "*":
ergebnis = zahl1 * zahl2;
return ergebnis;
}
return ergebnis;
}

public double meineNemesis() {

zahl1 = numbersDouble.get(0);

for (int i = 1; i <= numbersDouble.size(); i++)

{
op = operators.get(i - 1);

if (op.equals("=")) {
return zahl1;
}
zahl2 = numbersDouble.get(i);
zahl1 = rechnen(op, zahl1, zahl2);
}
return -80085;
}

public int berechneInt()
{
zahl1 = numbersInt.get(0);

for (int i = 1; i <= numbersInt.size(); i++)

{
op = operators.get(i - 1);

if (op.equals("=")) {
return (int)zahl1;
}
zahl2 = numbersInt.get(i);
zahl1 = rechnen(op, zahl1, zahl2);
}
return -80085 ;
}


public void addNumber(int i)
{
numbersInt.add(i);
}

public void addOperators(String op)
{
operators.add(op);
}

}

提前非常感谢

最佳答案

您的问题来自于未将 JFrame 和 JPanel 的布局设置为 null,因此 setBounds(...) 将生效。如果不通过这种方式明确设置布局,JPanel将盲目使用其默认的FlowLayout,而JFrame(实际上是其contentPane)将盲目使用其默认的BorderLayout

因此需要进行这些更改:

class PannelDez extends JPanel {
// .....

public PannelDez() {
// Panel
setLayout(null);

// ....
}

// ....
}

class Interface extends JFrame {

public Interface() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
// PanelAdden

// .....

}

// ..........

}

话虽如此,我重申我之前说过的话:

While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

再次强调,解决这个问题的更好方法是学习和使用布局管理器。例如,您正在创建一个计算器,这非常适合使用两个 JPanel,一个用于保存使用 GridLayout 的按钮以创建按钮网格,另一个使用 BorderLayout 来保存上述 JPanel并将 JTextField 保持在顶部位置,即 BorderLayout.PAGE_START 位置。另请注意,最好尽量避免代码重复,就像我们在所有 ActionListener 中看到的那样。您只需要两个 ActionListener - 每个数字按钮一个,每个操作一个。

下面的代码创建了一个如上所述的 Swing GUI,其中一个带有嵌套的 JPanel,外部使用 BorderLayout,内部使用 GridLayout(0, 4),它代表可变行数(0)和 4 列。 GUI 的图像:

enter image description here

代码:

导入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 {
private static final Font BUTTON_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 36);
private static final Font ANZEIGE_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 24);
private JTextField anzeige = new JTextField(10);
RechnerInt interfaceRechner = new RechnerInt();

public FooGui02() {
anzeige.setFont(ANZEIGE_FONT);
// anzeige.setEnabled(false);
anzeige.setBackground(null);
anzeige.setFocusable(false); // user can't interact with this

// create a grid of 4 columns and variable rows:
JPanel btnPanel = new JPanel(new GridLayout(0, 4));

// create first row
// add 3 blank JLabels
for (int i = 0; i < 3; i++) {
btnPanel.add(new JLabel());
}
btnPanel.add(createOperationButton(Operation.DIVISION));

// second row
btnPanel.add(createNumberButton(7));
btnPanel.add(createNumberButton(8));
btnPanel.add(createNumberButton(9));
btnPanel.add(createOperationButton(Operation.MULTIPLICATION));

// third row
btnPanel.add(createNumberButton(4));
btnPanel.add(createNumberButton(5));
btnPanel.add(createNumberButton(6));
btnPanel.add(createOperationButton(Operation.SUBTRACTION));

// forth row
btnPanel.add(createNumberButton(1));
btnPanel.add(createNumberButton(2));
btnPanel.add(createNumberButton(3));
btnPanel.add(createOperationButton(Operation.ADDITION));

// fifth row
btnPanel.add(new JLabel());
btnPanel.add(createNumberButton(0));
btnPanel.add(createOperationButton(Operation.CLEAR));
btnPanel.add(createOperationButton(Operation.EQUALS));

setLayout(new BorderLayout());
add(anzeige, BorderLayout.PAGE_START);
add(btnPanel);
}

private JButton createOperationButton(Operation operation) {
JButton button = new JButton(operation.getText());
button.addActionListener(new OperationListener(operation));
button.setFont(BUTTON_FONT);
return button;
}

private JButton createNumberButton(int number) {
JButton button = new JButton("" + number);
button.addActionListener(new NumberListener(number));
button.setFont(BUTTON_FONT);
return button;
}

private class OperationListener implements ActionListener {
private Operation operation;

public OperationListener(Operation operation) {
this.operation = operation;
}

@Override
public void actionPerformed(ActionEvent e) {
String text = anzeige.getText();

switch (operation) {
case CLEAR:
interfaceRechner.operators.clear();
interfaceRechner.numbers.clear();
interfaceRechner.numbersInt.clear();
anzeige.setText("");
break;

case ADDITION:
interfaceRechner.addNumber(Integer.valueOf(text));
interfaceRechner.addOperators("+");
anzeige.setText("");
break;

case SUBTRACTION:
interfaceRechner.addNumber(Integer.valueOf(text));
interfaceRechner.addOperators("-");
anzeige.setText("");
break;

case MULTIPLICATION:
interfaceRechner.addNumber(Integer.valueOf(text));
interfaceRechner.addOperators("*");
anzeige.setText("");
break;

case DIVISION:
interfaceRechner.addNumber(Integer.valueOf(text));
interfaceRechner.addOperators("/");
anzeige.setText("");
break;

case EQUALS:
interfaceRechner.addNumber(Integer.valueOf(text));
interfaceRechner.addOperators("=");
anzeige.setText("" + interfaceRechner.berechneInt());
interfaceRechner.operators.clear();
interfaceRechner.numbers.clear();
interfaceRechner.numbersInt.clear();
break;

default:
break;
}
}
}

private class NumberListener implements ActionListener {
private int number;

public NumberListener(int number) {
this.number = number;
}

@Override
public void actionPerformed(ActionEvent e) {
String text = anzeige.getText();
text += number;
anzeige.setText(text);
}
}

private static void createAndShowGui() {
FooGui02 mainPanel = new FooGui02();

JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

enum Operation {
DIVISION("/"), MULTIPLICATION("*"), SUBTRACTION("-"),
ADDITION("+"), CLEAR("C"), EQUALS("=");

private String text;

private Operation(String text) {
this.text = text;
}

public String getText() {
return text;
}
}

关于java - JPanel不适应任何变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52765110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com