多个面板可访问的 JButton
JButton accessible for several Panels
我是 Java 编程的新手并且有 "beginner Question"。
我已经尝试查找是否有人已经遇到同样的问题,但我找不到任何 post 处于我的低水平。
我的问题:
其中一个有 2 个 JPanel(我们称之为 Panel1),我实现了一个 JTextField (field1) 和一个 JButton (button1)。在 Panel2 上,我想绘制一些东西,具体取决于第一个 JTextField 中使用的提示。
我的问题是我希望 Panel2 "see" 按钮 1(在 Panel1 中),但我无法做到。
任何人都可以给我一个提示,Youtube 视频或类似的东西,在那里我可以看到有人处理此类问题的一个很好的例子吗?
问候文图拉
P.S 我有一个想法,将值传递给 Panel2,它应该在其中绘制我想要的东西,这里是我的代码(我希望我 post 它正确):这里是 Panel1:
public class Panel1 extends JPanel
{
public JButton button = new JButton("OK");
public JTextField field1 = new JTextField(5);
public String name;int b;
public Panel1()
{
setBackground(Color.WHITE);
add(field1);
add(button);
ButtonListener listener = new ButtonListener(field1);
button.addActionListener(listener);
}
public Dimension getPreferredSize()
{
return new Dimension(400,400);
}
}
这里是 ButtonListener :
class ButtonListener implements ActionListener
{
Graphics g;
//Graphics2D g2 = (Graphics2D) g;
public Panel2 pan2 = new Panel2();
public String name;
public JTextField field1 = new JTextField();
int b;
public ButtonListener(JTextField field1)
{this.field1 = field1;}
public void actionPerformed(ActionEvent e)
{
String op = e.getActionCommand();
if(op.equals("OK"))
{
name = field1.getText();
try
{
b = Integer.parseInt(name);
JOptionPane.showMessageDialog(null,"Ihre Zahl = "+b);
pan2.setvalue(b,g);
}
catch(NumberFormatException ex) {JOptionPane.showMessageDialog(null,"Please enter real numbers!");}
}
}
}
最后是 Panel2,我想根据 Panel1 中给出的输入绘制东西(例如:我输入 10,他给我画了一个宽度为 10 或类似的矩形)
public class Panel2 extends JPanel
{
Graphics g;
Graphics2D g2 = (Graphics2D) g;
public int b;
public Panel2()
{
setBackground(Color.YELLOW);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g);
g2.setColor(Color.RED);
g2.fillRect(200, 200, 50, 50);
Paint2(g);
}
public void Paint2(Graphics g)
{
this.g = g;
Graphics2D g2 = (Graphics2D) g;
//super.paintComponent(g);
System.out.println("TEST2");
g2.setColor(Color.BLUE);
//System.out.println("TEST3");
g2.fillRect(10 , 10, 40, 40);
}
public void setvalue(int b, Graphics g)
{
this.b = b;
this.g = g;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.CYAN);
g2.fillRect(0, 0, 40, 40);
System.out.println("B ist gleich = "+b);
//Paint2(g2);
}
public Dimension getPreferredSize()
{
return new Dimension(400,400);
}
如果读起来有点繁琐,请见谅。
我在这段代码中遇到的基本问题是当我想调用方法
时 Panel2 中的 NullpointerException
setvalue(int b, Graphics g)
g2.setColor(Color.Cyan)
你好帕特里克
我对 Panel1
的大部分进行了重新设计,对 Panel2
进行了一些重新设计。
我将从 Panel2 开始,因为它更简单。这是新代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Panel2 extends JPanel {
Graphics g;
Graphics2D g2;
public int b;
public Panel2() {
setBackground(Color.YELLOW);
b = 50;
}
@Override
public void paintComponent(Graphics g) {
g2 = (Graphics2D) g;
super.paintComponent(g);
g2.setColor(Color.RED);
g2.fillRect(200, 200, 50, 50);
g2.setColor(Color.BLUE);
g2.fillRect(10, 10, 40, 40);
g2.setColor(Color.CYAN);
g2.fillRect(b, b, 40, 40);
}
public void setValue(int b) {
this.b = b;
System.out.println("B ist gleich = " + b);
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
我对 Panel2 所做的最大改变是我制作了 g2
一个所有方法都可以使用的 class 变量。我没有让这些方法每次都使用一个 Graphics 变量,而是让它们可以使用 class 中的那个。每次调用该方法时,我也没有创建新的 Graphics2D 变量 "equal" 任何东西,因为没有必要这样做。我更新它的唯一地方是 paintComponent()
方法,这样我就可以为整个 class.
更新一次
接下来是 Panel1。这是代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Panel1 extends JPanel {
public JButton button = new JButton("OK");
public JTextField field1 = new JTextField(5);
public String name;
int b;
private Panel2 pan2;
public Panel1() {
setBackground(Color.WHITE);
add(field1);
add(button);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String op = e.getActionCommand();
if (op.equals("OK")) {
name = field1.getText();
try {
b = Integer.parseInt(name);
pan2.setValue(b);
JOptionPane.showMessageDialog(null, "Ihre Zahl = " + b);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter real numbers!");
}
}
}
};
button.addActionListener(listener);
}
public void setSecondPanel(Panel2 panel) {
pan2 = panel;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Panel1 pan1 = new Panel1();
Panel2 pan2 = new Panel2();
pan1.setSecondPanel(pan2);
frame.setSize(pan1.getPreferredSize().width + pan2.getPreferredSize().width, pan1.getPreferredSize().height);
frame.add(pan2, BorderLayout.EAST);
frame.add(pan1, BorderLayout.WEST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
首先,请注意我没有创建 ButtonListener
。因为你只使用它一次,所以不需要为它创建一个新的class(它也更少代码)。
在 main()
方法中,我这样做是为了将两个面板添加到相对的两侧,这样它们就不会相互重叠或发生任何重叠。
最后,我在 Panel1
class 中创建了一个 setSecondPanel
方法,这样 class 就会有一个 Panel2
变量可以随时使用时间。
我是 Java 编程的新手并且有 "beginner Question"。 我已经尝试查找是否有人已经遇到同样的问题,但我找不到任何 post 处于我的低水平。
我的问题: 其中一个有 2 个 JPanel(我们称之为 Panel1),我实现了一个 JTextField (field1) 和一个 JButton (button1)。在 Panel2 上,我想绘制一些东西,具体取决于第一个 JTextField 中使用的提示。
我的问题是我希望 Panel2 "see" 按钮 1(在 Panel1 中),但我无法做到。
任何人都可以给我一个提示,Youtube 视频或类似的东西,在那里我可以看到有人处理此类问题的一个很好的例子吗?
问候文图拉
P.S 我有一个想法,将值传递给 Panel2,它应该在其中绘制我想要的东西,这里是我的代码(我希望我 post 它正确):这里是 Panel1:
public class Panel1 extends JPanel
{
public JButton button = new JButton("OK");
public JTextField field1 = new JTextField(5);
public String name;int b;
public Panel1()
{
setBackground(Color.WHITE);
add(field1);
add(button);
ButtonListener listener = new ButtonListener(field1);
button.addActionListener(listener);
}
public Dimension getPreferredSize()
{
return new Dimension(400,400);
}
}
这里是 ButtonListener :
class ButtonListener implements ActionListener
{
Graphics g;
//Graphics2D g2 = (Graphics2D) g;
public Panel2 pan2 = new Panel2();
public String name;
public JTextField field1 = new JTextField();
int b;
public ButtonListener(JTextField field1)
{this.field1 = field1;}
public void actionPerformed(ActionEvent e)
{
String op = e.getActionCommand();
if(op.equals("OK"))
{
name = field1.getText();
try
{
b = Integer.parseInt(name);
JOptionPane.showMessageDialog(null,"Ihre Zahl = "+b);
pan2.setvalue(b,g);
}
catch(NumberFormatException ex) {JOptionPane.showMessageDialog(null,"Please enter real numbers!");}
}
}
}
最后是 Panel2,我想根据 Panel1 中给出的输入绘制东西(例如:我输入 10,他给我画了一个宽度为 10 或类似的矩形)
public class Panel2 extends JPanel
{
Graphics g;
Graphics2D g2 = (Graphics2D) g;
public int b;
public Panel2()
{
setBackground(Color.YELLOW);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g);
g2.setColor(Color.RED);
g2.fillRect(200, 200, 50, 50);
Paint2(g);
}
public void Paint2(Graphics g)
{
this.g = g;
Graphics2D g2 = (Graphics2D) g;
//super.paintComponent(g);
System.out.println("TEST2");
g2.setColor(Color.BLUE);
//System.out.println("TEST3");
g2.fillRect(10 , 10, 40, 40);
}
public void setvalue(int b, Graphics g)
{
this.b = b;
this.g = g;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.CYAN);
g2.fillRect(0, 0, 40, 40);
System.out.println("B ist gleich = "+b);
//Paint2(g2);
}
public Dimension getPreferredSize()
{
return new Dimension(400,400);
}
如果读起来有点繁琐,请见谅。 我在这段代码中遇到的基本问题是当我想调用方法
时 Panel2 中的 NullpointerExceptionsetvalue(int b, Graphics g) g2.setColor(Color.Cyan)
你好帕特里克
我对 Panel1
的大部分进行了重新设计,对 Panel2
进行了一些重新设计。
我将从 Panel2 开始,因为它更简单。这是新代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Panel2 extends JPanel {
Graphics g;
Graphics2D g2;
public int b;
public Panel2() {
setBackground(Color.YELLOW);
b = 50;
}
@Override
public void paintComponent(Graphics g) {
g2 = (Graphics2D) g;
super.paintComponent(g);
g2.setColor(Color.RED);
g2.fillRect(200, 200, 50, 50);
g2.setColor(Color.BLUE);
g2.fillRect(10, 10, 40, 40);
g2.setColor(Color.CYAN);
g2.fillRect(b, b, 40, 40);
}
public void setValue(int b) {
this.b = b;
System.out.println("B ist gleich = " + b);
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
我对 Panel2 所做的最大改变是我制作了 g2
一个所有方法都可以使用的 class 变量。我没有让这些方法每次都使用一个 Graphics 变量,而是让它们可以使用 class 中的那个。每次调用该方法时,我也没有创建新的 Graphics2D 变量 "equal" 任何东西,因为没有必要这样做。我更新它的唯一地方是 paintComponent()
方法,这样我就可以为整个 class.
接下来是 Panel1。这是代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Panel1 extends JPanel {
public JButton button = new JButton("OK");
public JTextField field1 = new JTextField(5);
public String name;
int b;
private Panel2 pan2;
public Panel1() {
setBackground(Color.WHITE);
add(field1);
add(button);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String op = e.getActionCommand();
if (op.equals("OK")) {
name = field1.getText();
try {
b = Integer.parseInt(name);
pan2.setValue(b);
JOptionPane.showMessageDialog(null, "Ihre Zahl = " + b);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter real numbers!");
}
}
}
};
button.addActionListener(listener);
}
public void setSecondPanel(Panel2 panel) {
pan2 = panel;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Panel1 pan1 = new Panel1();
Panel2 pan2 = new Panel2();
pan1.setSecondPanel(pan2);
frame.setSize(pan1.getPreferredSize().width + pan2.getPreferredSize().width, pan1.getPreferredSize().height);
frame.add(pan2, BorderLayout.EAST);
frame.add(pan1, BorderLayout.WEST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
首先,请注意我没有创建 ButtonListener
。因为你只使用它一次,所以不需要为它创建一个新的class(它也更少代码)。
在 main()
方法中,我这样做是为了将两个面板添加到相对的两侧,这样它们就不会相互重叠或发生任何重叠。
最后,我在 Panel1
class 中创建了一个 setSecondPanel
方法,这样 class 就会有一个 Panel2
变量可以随时使用时间。