{ JPanel buttonPane = new JPanel(); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); getContentPane().add(buttonPane, BorderLayout.SOUTH); { JButton okButton = new JButton("OK"); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String bianhao=(String) textField_1.getText(); String xm=(String) textField_2.getText(); String nl=(String) textField_3.getText(); String jg=(String) textField_4.getText(); String bz=(String) textField_5.getText(); String[] str = {bianhao,xm,nl,jg,bz}; try { FileWriter w=new FileWriter("chufang.txt",true); //true追加模式 FileReader r=new FileReader("chufang.txt"); BufferedWriter out=new BufferedWriter(w); BufferedReader in=new BufferedReader(r); for(int i =0; i<str.length; i++){ out.write(str[i]); //向文件中写入数据 out.write(' '); // 空格分隔 } out.write('\n'); out.close(); in.close(); } catch (IOException ioException) { ioException.printStackTrace(); } dispose(); } }); okButton.setActionCommand("OK"); buttonPane.add(okButton); getRootPane().setDefaultButton(okButton); } } setVisible(true); }
时间: 2024-04-18 18:32:08 浏览: 20
这段代码是在对话框中添加了一个面板(buttonPane),并在面板中添加了一个按钮(okButton)。
首先,创建了一个名为buttonPane的JPanel对象,并将其添加到对话框的南边(BorderLayout.SOUTH)。
然后,设置buttonPane的布局管理器为右对齐的流式布局(FlowLayout.RIGHT)。
接下来,在buttonPane中创建了一个按钮okButton,并设置了按钮的文本为"OK"。
为okButton添加了一个动作监听器(ActionListener),当点击按钮时,执行监听器中的actionPerformed方法。
在actionPerformed方法中,获取文本字段textField_1、textField_2、textField_3、textField_4、textField_5的文本内容,并将其分别赋值给变量bianhao、xm、nl、jg、bz。
然后,将这些文本内容保存到文件"chufang.txt"中。首先创建了FileWriter和FileReader对象,然后使用BufferedWriter向文件中写入数据,使用BufferedReader关闭文件输入流和输出流。
最后,在actionPerformed方法中调用dispose方法关闭对话框。
在构造方法的末尾,调用setVisible方法将对话框设置为可见状态。
相关问题
JPanel buttonPane = new JPanel(); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); getContentPane().add(buttonPane, BorderLayout.SOUTH);
这段代码是在创建一个 JPanel 对象,并将其添加到 GUI 窗口的底部(即南部)。该 JPanel 使用 FlowLayout 布局管理器,并设置为右对齐。
接下来,通过调用 getContentPane() 方法获取到窗口的内容面板(Container),并使用 BorderLayout 布局管理器将 buttonPane 添加到内容面板的南部。
通过这样的布局设置,可以将该 JPanel 放置在窗口的底部,并使用 FlowLayout 使其中的组件按照从右到左的顺序排列。
你可以在这个 JPanel 中添加按钮或其他 GUI 组件,以实现窗口底部的交互功能。例如,可以在 buttonPane 中添加一个 JButton:
```java
JButton okButton = new JButton("OK");
buttonPane.add(okButton);
```
这样,一个带有一个 "OK" 按钮的 JPanel 就会显示在窗口底部,并按照从右到左的顺序对齐。
在fram里添加一个控件显示透明背景,不遮挡文本框和内容 frame = new JFrame("点名教师端"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 500); frame.getContentPane().setLayout(new FlowLayout()); JLabel l1=new JLabel("端口号:"); //frame.add(l1); textFieldPort = new JTextField(20); frame.add(textFieldPort);
要实现透明背景的控件,可以使用JLabel或JPanel作为容器,并将其设置为透明。在JLabel或JPanel中添加其他控件即可。以下是示例代码:
```
frame = new JFrame("点名教师端");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500);
frame.getContentPane().setLayout(new FlowLayout());
JLabel transparentLabel = new JLabel();
transparentLabel.setOpaque(false); // 设置为透明
transparentLabel.setLayout(new FlowLayout()); // 设置布局方式
JLabel l1=new JLabel("端口号:");
transparentLabel.add(l1);
textFieldPort = new JTextField(20);
transparentLabel.add(textFieldPort);
frame.add(transparentLabel);
```
这样就可以在窗口中添加一个透明背景的控件,并在其中添加其他控件,不会遮挡文本框和内容。