【发布时间】:2021-03-15 13:57:52
【问题描述】:
我正在尝试将我的 java 文件发送到其中我有一个带有按钮的 JFrame 绘图,该按钮将背景颜色从客户端更改为服务器。服务器收到该绘图并打开它,但是当我单击按钮时没有任何变化。我究竟做错了什么?另外由于某种原因,应用程序有时无法运行。
绘图代码
package Drawings;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class DrawingTwo extends JFrame {
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;
public Color tvColor;
public Color smileColor;
public int h;
public int h2;
public int h3;
public int h4;
public int h5;
public int h6;
public String l;
public DrawComponent c;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new DrawingTwo();
}
});
}
public DrawingTwo() {
super();
setOneChanell();
Container container = getContentPane();
container.setBackground(new Color(242, 212, 252));
container.setLayout(new BorderLayout(20, 20));
container.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
ButtonListener listener = new ButtonListener();
JLabel lb = new JLabel(l);
lb.setText(l);
container.add(lb);
JButton j2 = new JButton("2");
j2.addActionListener(listener);
container.add(j2, BorderLayout.NORTH);
c = new DrawComponent();
container.add(c, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void setOneChanell() {
this.tvColor = new Color(255, 153, 153);
this.smileColor = new Color(255, 247, 10);
this.h = 110;
this.h2 = 55;
this.h3 = 60;
this.h4 = 60;
this.h5 = 0;
this.h6 = -180;
this.l = "Канал для веселых";
}
public void setTwoChanell() {
this.tvColor = new Color(172, 194, 157);
this.smileColor = new Color(0, 161, 219);
this.h = 115;
this.h2 = 87;
this.h3 = 50;
this.h4 = 40;
this.h5 = 0;
this.h6 = +180;
this.l = "Канал для грустных";
}
public class DrawComponent extends JComponent {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(37, 26, 210, 130);
g.setColor(tvColor);
g.fillRect(42, 30, 200, 120);
g.setColor(Color.darkGray);
g.fillRect(135, 156, 15, 20);
g.setColor(Color.darkGray);
g.fillRect(83, 170, 120, 13);
g.setColor(smileColor);
g.fillOval(100, 45, 80, 80);
g.setColor(Color.BLACK);
g.drawArc(120, 70, 10, 10, 0, 360);
g.drawArc(150, 70, 10, 10, 0, 360);
g.drawString(l, 83, 200);
g.setColor(Color.BLACK);
g.drawArc(h, h2, h3, h4, h5, h6);
}
}
public class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
if (button.getText().equals("2")) {
setTwoChanell();
button.setText("1");
} else {
setOneChanell();
button.setText("2");
}
c.repaint();
}
}
}
这是客户端文件
package ClientToServer;
import java.io.*;
import java.net.Socket;
import Drawings.DrawingTwo;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 12345);
OutputStream outputStream = socket.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(new DrawingTwo());
objectOutputStream.flush();
objectOutputStream.close();
}
}
这是服务器文件
package ClientToServer;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import Drawings.DrawingTwo;
import javax.swing.*;
public class Server {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ServerSocket serverSocket = new ServerSocket(12345);
Socket client = serverSocket.accept();
ObjectInputStream inputStream = new ObjectInputStream(client.getInputStream());
DrawingTwo object = (DrawingTwo) inputStream.readObject();
object.setVisible(true);
object.setTitle("Server");
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.close();
inputStream.close();
serverSocket.close();
}
}
【问题讨论】:
-
我做错了什么? 除此之外:仅在event dispatch thread 上构造和操作Swing GUI 对象。 “Swing 程序应该覆盖
paintComponent()而不是覆盖paint()。”—Painting in AWT and Swing: The Paint Methods。 -
您需要在服务器端重新附加/重新建立按钮侦听器。
-
Drawing2构造函数在客户端运行,这意味着其中包含的所有初始化对客户端都是有意义的。我不确定你想通过将此对象发送到另一台设备来做什么。 -
@trashgod 对绘图文件进行了一些更改
-
@PresidentJamesK.Polk 这是我的课,只是其中一项任务