public void onclick00(MouseEvent mouseEvent) throws IOException { // 第二界面 Service.method=00; Parent root2 = FXMLLoader.load(getClass().getResource("/controller/StartView.fxml")); JFrame frame = new JFrame();// 创建一个窗口 frame.setTitle("显示确认");// 设置窗口标题 frame.setBounds(200, 100, 500, 300);// 设置窗口位置和大小 frame.setLayout(null);// 布局为空 JPanel panel1 = new JPanel(); JLabel labTitle = new JLabel("哲学家人数已改变");//设置窗口标题 Font font1 = new Font("宋体", Font.BOLD, 20);//设置字形,字体和字号 labTitle.setFont(font1); panel1.add(labTitle); panel1.setBounds(80, 20, 300, 80);//设置窗口位置和大小 frame.add(panel1); frame.setVisible(true);// 显示窗口 }这段代码能不能获取text文本框中的值
时间: 2024-03-10 20:44:49 浏览: 172
这段代码并没有显示出文本框,因此无法直接获取文本框中的值。需要先在界面中添加文本框,然后在代码中通过相应的方法获取文本框的值。可以使用FXML文件来定义界面,然后在Controller中进行操作。例如,在FXML文件中添加一个文本框:
```xml
<TextField fx:id="textField" layoutX="50.0" layoutY="50.0" prefWidth="200.0" />
```
然后在Controller中获取文本框的值,可以在onclick00方法中添加以下代码:
```java
TextField textField = (TextField)root2.lookup("#textField"); // 获取FXML文件中定义的文本框
String text = textField.getText(); // 获取文本框中的值
```
这样就可以获取到文本框中的值了。
相关问题
1. 编写图形用户界面,教师为服务器端,学生为客户端,一个服务端可以接受多个客户端连接。 2. 要求完成教师白板的建立,实现教师在白板上画图,学生端能够实时刷新。 3. 教师可以传输文件给学生。 4. 学生可以向教师提问,问答内容所有学生都能看到。实现以上要求
这是一个比较复杂的项目,需要涉及到网络编程、GUI编程、多线程编程等方面的知识。下面是一个简单的实现方案供参考:
1. 编写服务器端代码:
```java
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
public class WhiteboardServer {
private List<ClientHandler> clients = new ArrayList<>();
private Whiteboard whiteboard = new Whiteboard();
public static void main(String[] args) throws IOException {
new WhiteboardServer().start();
}
public void start() throws IOException {
ServerSocket serverSocket = new ServerSocket(4444);
System.out.println("Server started.");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket);
ClientHandler clientHandler = new ClientHandler(clientSocket);
clients.add(clientHandler);
clientHandler.start();
}
}
public synchronized void broadcastMessage(String message) {
for (ClientHandler client : clients) {
client.sendMessage(message);
}
}
public synchronized void broadcastImage(Image image) {
for (ClientHandler client : clients) {
client.sendImage(image);
}
}
public synchronized void broadcastFile(File file) throws IOException {
for (ClientHandler client : clients) {
client.sendFile(file);
}
}
public synchronized void removeClient(ClientHandler client) {
clients.remove(client);
}
private class ClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public ClientHandler(Socket socket) throws IOException {
clientSocket = socket;
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public void run() {
try {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Received message from client: " + inputLine);
if (inputLine.startsWith("DRAW")) {
whiteboard.draw(inputLine.substring(5));
broadcastMessage(inputLine);
} else if (inputLine.startsWith("FILE")) {
File file = new File(inputLine.substring(5));
if (file.exists()) {
broadcastFile(file);
}
} else if (inputLine.equals("QUESTION")) {
String question = in.readLine();
System.out.println("Received question from client: " + question);
broadcastMessage("ANSWER " + question);
}
}
} catch (IOException e) {
System.err.println("Error handling client: " + e);
} finally {
out.close();
try {
in.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("Error closing client socket: " + e);
}
removeClient(this);
}
}
public void sendMessage(String message) {
out.println(message);
}
public void sendImage(Image image) {
// TODO: implement this method
}
public void sendFile(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
clientSocket.getOutputStream().write(buffer, 0, bytesRead);
}
fis.close();
}
}
}
```
2. 编写客户端代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class WhiteboardClient {
private Socket socket;
private PrintWriter out;
private BufferedReader in;
private WhiteboardCanvas canvas;
public static void main(String[] args) throws IOException {
new WhiteboardClient().start();
}
public void start() throws IOException {
String serverAddress = JOptionPane.showInputDialog("Enter server IP address:");
socket = new Socket(serverAddress, 4444);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
canvas = new WhiteboardCanvas();
JFrame frame = new JFrame("Whiteboard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(canvas, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
new Thread(new ServerListener()).start();
canvas.addMouseListener(new DrawingListener());
canvas.addMouseMotionListener(new DrawingListener());
canvas.requestFocus();
}
private void draw(String message) {
String[] parts = message.split(" ");
int x1 = Integer.parseInt(parts[0]);
int y1 = Integer.parseInt(parts[1]);
int x2 = Integer.parseInt(parts[2]);
int y2 = Integer.parseInt(parts[3]);
canvas.drawLine(x1, y1, x2, y2);
}
private void receiveImage() {
// TODO: implement this method
}
private void receiveFile() throws IOException {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(canvas);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = socket.getInputStream().read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fos.close();
}
}
private void askQuestion() throws IOException {
String question = JOptionPane.showInputDialog("Enter your question:");
out.println("QUESTION");
out.println(question);
}
private class ServerListener implements Runnable {
public void run() {
try {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Received message from server: " + inputLine);
if (inputLine.startsWith("DRAW")) {
draw(inputLine.substring(5));
} else if (inputLine.startsWith("IMAGE")) {
receiveImage();
} else if (inputLine.startsWith("FILE")) {
receiveFile();
} else if (inputLine.startsWith("ANSWER")) {
String question = inputLine.substring(7);
JOptionPane.showMessageDialog(canvas, "The answer is: " + question);
}
}
} catch (IOException e) {
System.err.println("Error receiving message from server: " + e);
} finally {
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {
System.err.println("Error closing client socket: " + e);
}
}
}
}
private class DrawingListener extends MouseAdapter {
private int x, y;
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
public void mouseDragged(MouseEvent e) {
int x2 = e.getX();
int y2 = e.getY();
out.println("DRAW " + x + " " + y + " " + x2 + " " + y2);
canvas.drawLine(x, y, x2, y2);
x = x2;
y = y2;
}
}
}
```
3. 编写画板代码:
```java
import java.awt.*;
import javax.swing.*;
public class WhiteboardCanvas extends JPanel {
private Image image;
private Graphics2D g2;
public WhiteboardCanvas() {
setPreferredSize(new Dimension(800, 600));
setBackground(Color.WHITE);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image == null) {
image = createImage(getWidth(), getHeight());
g2 = (Graphics2D) image.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}
public void clear() {
g2.setPaint(Color.WHITE);
g2.fillRect(0, 0, getWidth(), getHeight());
repaint();
}
public void drawLine(int x1, int y1, int x2, int y2) {
g2.drawLine(x1, y1, x2, y2);
repaint();
}
}
```
在实现以上代码之后,就可以在教师端启动服务器,学生端启动客户端,然后进行画图、发送文件、提问等操作了。需要注意的是,在实现传输图片的功能时,可以使用Java的ImageIO类来将图像转换成字节数组,然后在网络上传输,接收方可以将字节数组转换成图像。
用Java代码编写一个程序实现一下功能 1)从已有的图象文件列表中选择某一个,即可显示该图象,并可做旋转等变换。 2)在画布区域上可以进行各种规则图形的绘制(如:线段、矩形、多边形、圆等),还可通过鼠标的拖动自由绘图。 3)可以连续播放所选文件夹下的所有图象。
好的,以下是使用Java代码实现功能的示例:
1. 显示并旋转图像
```java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageDisplay extends JPanel {
private Image image;
private double angle = 0;
public ImageDisplay(String imagePath) throws IOException {
image = ImageIO.read(new File(imagePath));
}
public void rotate(double angle) {
this.angle += angle;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
AffineTransform transform = new AffineTransform();
transform.translate(getWidth() / 2, getHeight() / 2);
transform.rotate(Math.toRadians(angle), 0, 0);
transform.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
g2d.drawImage(image, transform, null);
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Image Display");
ImageDisplay panel = new ImageDisplay("image.jpg");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
panel.rotate(45); // 旋转45度
}
}
```
2. 绘制图形
```java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShapeDrawer extends JPanel {
private ArrayList<Point> points = new ArrayList<Point>();
private ArrayList<Shape> shapes = new ArrayList<Shape>();
private Shape currentShape = null;
public ShapeDrawer() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
if (currentShape != null) {
shapes.add(currentShape);
currentShape = null;
}
points.clear();
repaint();
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (points.size() > 0) {
Point lastPoint = points.get(points.size() - 1);
currentShape = new Line(lastPoint, e.getPoint());
repaint();
}
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Shape shape : shapes) {
shape.draw(g2d);
}
if (currentShape != null) {
currentShape.draw(g2d);
}
}
private abstract class Shape {
public abstract void draw(Graphics2D g);
}
private class Line extends Shape {
private Point start;
private Point end;
public Line(Point start, Point end) {
this.start = start;
this.end = end;
}
@Override
public void draw(Graphics2D g) {
g.drawLine(start.x, start.y, end.x, end.y);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Shape Drawer");
ShapeDrawer panel = new ShapeDrawer();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
```
3. 连续播放图像
```java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ImagePlayer extends JPanel {
private Image[] images;
private int currentIndex = 0;
private double angle = 0;
public ImagePlayer(String folderPath) throws IOException {
File folder = new File(folderPath);
File[] imageFiles = folder.listFiles((dir, name) -> name.endsWith(".jpg"));
images = new Image[imageFiles.length];
for (int i = 0; i < imageFiles.length; i++) {
images[i] = ImageIO.read(imageFiles[i]);
}
Timer timer = new Timer(1000, e -> {
currentIndex = (currentIndex + 1) % images.length;
repaint();
});
timer.start();
}
public void rotate(double angle) {
this.angle += angle;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Image image = images[currentIndex];
AffineTransform transform = new AffineTransform();
transform.translate(getWidth() / 2, getHeight() / 2);
transform.rotate(Math.toRadians(angle), 0, 0);
transform.translate(-image.getWidth(null) / 2, -image.getHeight(null) / 2);
g2d.drawImage(image, transform, null);
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Image Player");
ImagePlayer panel = new ImagePlayer("images");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
panel.rotate(45); // 旋转45度
}
}
```
阅读全文