使用Java Swing时实现Runnable

现在学习了一些Swing,发现了两个教程,它们使用了不同的方法来制作简单的JFrame窗口.

 

第一个实现Runnable并在类中具有JFrame对象变量:

 

class SwingDemo implements Runnable {
    private JFrame frame;

    @Override
    public void run() {
        frame = new JFrame("title");

        ... // setSize(), add components, etc

    }
}

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SwingDemo());
    }
}

第二个教程没有实现Runnable,而是使用类构造函数初始化JFrame并通过匿名内部类调用该构造函数

 

class SwingDemoAlt {

    public SwingDemoAlt() {
        JFrame frame = new JFrame("title");

        ... // again some code here

    }
}

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingDemoAlt();
            }
        }
    }
}

两种方式有何不同?一种方法更可取吗?

最佳答案

How do the two ways differ?

他们并不是,不是真的,他们基本上是通过不同的方式实现同​​一件事.

第一种是更“传统”的方法,第二种是更“现代”的或简化的方法,该方法利用了Anonymous Classes语言的受欢迎程度.

 

And is there a more preferred choice?

这是一个观点,对我来说,第二个是首选,因为它不会在类上添加Runnable的其他不必要的一致性,它还委派了在调用方上正确设置UI的责任,并停止了代码通过做出假设(即,您可以在运行的任何时候简单地构造框架…只需在事件调度线程的上下文中进行操作即可).

另外,作为首选项,您不应直接从JFrame扩展,因为您实际上并未在类中添加新功能,而是像第二个示例中所做的那样,仅在需要时创建实例并构建UI在它的上面

您可能还希望查看Concurrency in Swing,以获取有关为什么应使用EventQueue.invokeLater启动UI的更多详细信息.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用java Swing实现文件上传操作的代码如下:JFileChooser fileChooser = new JFileChooser(); int returnValue = fileChooser.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); // 使用选定的文件来执行上传操作 } ### 回答2: 使用Java Swing实现文件上传操作的代码如下: ```java import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; public class FileUploadExample extends JFrame { private JButton chooseFileButton; private JLabel selectedFileLabel; public FileUploadExample() { setTitle("文件上传示例"); setSize(400, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); chooseFileButton = new JButton("选择文件"); selectedFileLabel = new JLabel("未选择文件"); chooseFileButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(FileUploadExample.this); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); selectedFileLabel.setText("选择的文件:" + selectedFile.getName()); // 实现文件上传的逻辑代码 // ... } } }); JPanel panel = new JPanel(); panel.add(chooseFileButton); panel.add(selectedFileLabel); add(panel); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { FileUploadExample example = new FileUploadExample(); example.setVisible(true); } }); } } ``` 这个例子中创建了一个简单的Swing应用程序窗口,里面包括一个按钮"选择文件"和一个标签"未选择文件"。当用户点击"选择文件"按钮后,会弹出一个文件选择对话框,用户可以选择要上传的文件。选择文件后,文件名会显示在标签上,并可以在代码中进行文件上传操作。 需要注意的是,这里的文件上传操作是一个占位符,具体的上传逻辑需要根据具体需求来实现。 ### 回答3: 在Java Swing实现文件上传操作的代码可以参考以下示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; public class FileUploadExample extends JFrame implements ActionListener { private JTextField textField; private JButton browseButton; private JButton uploadButton; public FileUploadExample() { // 设置窗口标题 setTitle("文件上传示例"); // 创建文本框和按钮 textField = new JTextField(20); browseButton = new JButton("浏览"); uploadButton = new JButton("上传"); // 设置布局管理器 setLayout(new FlowLayout()); getContentPane().add(textField); getContentPane().add(browseButton); getContentPane().add(uploadButton); // 添加事件监听器 browseButton.addActionListener(this); uploadButton.addActionListener(this); // 设置窗口大小和关闭操作 setSize(300, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == browseButton) { // 选择文件 JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); textField.setText(selectedFile.getAbsolutePath()); } } else if (e.getSource() == uploadButton) { // 上传文件 String filePath = textField.getText(); File file = new File(filePath); if (file.exists()) { // 执行上传操作,此处省略具体的文件上传代码 JOptionPane.showMessageDialog(this, "文件上传成功"); } else { JOptionPane.showMessageDialog(this, "文件不存在"); } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new FileUploadExample().setVisible(true); } }); } } ``` 以上代码创建了一个简单的文件上传窗口,用户可以通过浏览按钮选择要上传的文件,并通过上传按钮执行文件上传操作。实际的文件上传操作需要根据具体需求进行实现。这里只是通过弹出消息框来模拟文件上传的反馈结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值