gpt4 book ai didi

java - 使用 eclipse 在 Java 中编写 GUI 应用程序

转载 作者:行者123 更新时间:2023-12-01 10:46:58 24 4
gpt4 key购买 nike

这就是我当前项目的注释中给出的示例。通常我会运行示例代码并使用它来查看一切是如何工作的。但是我不确定如何正确调用示例代码中的函数。

这是给我的:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Draw extends JPanel implements ActionListener
{
JTextField tfInfo;
JLabel lblColor, lblShapes;
JCheckBox cbRed, cbBlue;
ButtonGroup shapes;
JRadioButton rbCircle, rbSquare;
JButton btnSubmit;
public Draw()
{
setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
tfInfo = new JTextField("Color & Shapes", 15);
lblColor = new JLabel("Colors:");
cbRed = new JCheckBox("Red");
cbBlue = new JCheckBox("Blue");
lblShapes = new JLabel("Shapes:");
shapes = new ButtonGroup();
rbCircle = new JRadioButton("Circle");
rbSquare = new JRadioButton("Square");
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(this);
add(tfInfo);
add(lblColor);
add(cbRed);
add(cbBlue);
add(lblShapes);
add(rbCircle);
add(rbSquare);
add(btnSubmit);
shapes.add(rbCircle);
shapes.add(rbSquare);
}
public static void main(String [] args)
{
Draw n = new Draw();
n.setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == btnSubmit)
{
if(cbRed.isSelected()&&cbBlue.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("urple Circle");
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Purple Square");
}
}
else if(cbRed.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("Red Circle");
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Red Square");
}
}
else if(cbBlue.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("Blue Circle");
}
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Blue Square");
}
}
}
public class MApp extends JPanel implements MouseListener
{
private boolean clicked;
private Rectangle r;
public MApp()
{
clicked = false;
r = new Rectangle(10, 10, 50, 50);
addMouseListener(this);
}
public void paintComponent(Graphics g)
{
if(clicked)
{
g.setColor(Color.BLUE);
}
else
{
g.setColor(Color.RED);
}
g.fillRect((int)r.getX(), (int)r.getY(),
(int)r.getWidth(), (int)r.getHeight());
}
public void mouseClicked (MouseEvent e)
{
Point p = new Point(e.getX(),e.getY());
if(r.contains(p))
{
clicked = !clicked;
}
repaint();
}
public void mousePressed (MouseEvent evnt) {}
public void mouseReleased (MouseEvent evnt) {}
public void mouseEntered (MouseEvent evnt) {}
public void mouseExited (MouseEvent evnt) {}
}
}

最佳答案

我想我会把它分成以下几个部分:

1) 将 MApp 放入它自己的 java 文件中。另外,我将 mouseClicked 中的 repaint() 更改为:paintComponent(getGraphics());您的新 java 文件将如下所示:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

public class MApp extends JPanel implements MouseListener
{
private boolean clicked;
private Rectangle r;
public MApp()
{
clicked = false;
r = new Rectangle(10, 10, 50, 50);
addMouseListener(this);
}
public void paintComponent(Graphics g)
{
if(clicked)
{
g.setColor(Color.BLUE);
}
else
{
g.setColor(Color.RED);
}
g.fillRect((int)r.getX(), (int)r.getY(),
(int)r.getWidth(), (int)r.getHeight());
}
public void mouseClicked (MouseEvent e)
{
Point p = new Point(e.getX(),e.getY());
if(r.contains(p))
{
clicked = !clicked;
}
paintComponent(getGraphics());
}
public void mousePressed (MouseEvent evnt) {}
public void mouseReleased (MouseEvent evnt) {}
public void mouseEntered (MouseEvent evnt) {}
public void mouseExited (MouseEvent evnt) {}
}

下一步:创建一个主文件,如下所示:

import javax.swing.JFrame;

public class Tester {

public static JFrame window = new JFrame("Graphics");

public static void main(String[] args) {
window.setBounds(100, 100,800, 800);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(null);

MApp m = new MApp();
m.setBounds(100,100,50,50);
window.add(m);

Draw d = new Draw();
d.setBounds(0, 0, window.getWidth(), 80);
window.add(d);

window.setVisible(true);
}

}

(不要忘记从 Draw 类中删除 MApp 类)

您将能够运行代码并查看正在发生的情况...这只是一个带有一些快捷方式的快速示例,但您会明白的

关于java - 使用 eclipse 在 Java 中编写 GUI 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34126033/

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