JFrame jFrame = new JFrame("论坛"); // 设置窗口的位置和大小 jFrame.setBounds(400, 300, 500, 500); // 设置窗口是否可见 jFrame.setVisible(true); // 设置窗口是否可以缩放 jFrame.setResizable(true); /** * 设置窗口的相对位置。 * 如果 comp 整个显示区域在屏幕内, 则将窗口放置到 comp 的中心; * 如果 comp 显示区域有部分不在屏幕内, 则将该窗口放置在最接近 comp 中心的一侧; * comp 为 null, 表示将窗口放置到屏幕中心。 */ jFrame.setLocationRelativeTo(null); JPanel jPanel = new JPanel(); JButton logIn = new JButton("Log In"); logIn.setVisible(true); jPanel.add(logIn); jFrame.setContentPane(jPanel);执行之后按钮看不到的原因
时间: 2023-07-16 22:12:44 浏览: 78
可能是因为按钮的大小没有被设置,或者是被其他组件遮挡了。你可以尝试给按钮设置大小,比如使用`logIn.setPreferredSize(new Dimension(100, 50));`来设置按钮大小。如果还看不到按钮,可以尝试使用透明度设置来检查是否被其他组件遮挡,比如使用`logIn.setOpaque(true); logIn.setBackground(Color.RED);`来将按钮背景设置为红色以便观察是否被其他组件遮挡。
相关问题
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文本框中的值
这段代码并没有显示出文本框,因此无法直接获取文本框中的值。需要先在界面中添加文本框,然后在代码中通过相应的方法获取文本框的值。可以使用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(); // 获取文本框中的值
```
这样就可以获取到文本框中的值了。
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JLabel; import java.awt.*; public class demo extends Frame{ public static void main(String[] args) { JFrame frame=new JFrame();//创建一个窗口 frame.setTitle("开始你的复制吧!");//设置窗口标题 frame.setBounds(400,400,400,400);//设置窗口的位置和大小 frame.setVisible(true);//设置Frame为可见,默认不可见 frame.setBackground(Color.CYAN);//设置Frame的背景色 //JPanel panel=new JPanel();//创建一个面板 JTextField textField1=new JTextField(30); JTextField textField2=new JTextField(30); JLabel label1=new JLabel("输入你要拷贝的文件:"); JLabel label2=new JLabel("输入拷到哪里去:"); textField1.setColumns(30); textField2.setColumns(30); label1.setBounds(10,10,100,20); label2.setBounds(10,40,100,20); textField1.setBounds(110,10,80,20); textField2.setBounds(110,40,50,20); frame.setLayout(null);//不适用布局管理器,设置为NULL frame.add(label1); frame.add(textField1); frame.add(label2); frame.add(textField2); //frame.setSize(400,100); //frame.setLocation(300,200); frame.setVisible(true); } }看一下哪里有问题,为什么生成的界面需要手动拉一下才显示文字和框
在这段代码中,你使用了 `setLayout(null)` 来将布局管理器设置为 `null`,这意味着你需要手动设置组件的位置和大小,否则它们将不会显示在你想要的位置。你需要调整组件的位置和大小,使它们适合窗口的大小。
另外,你创建了一个 `Frame` 对象,但是你没有使用它,而是创建了一个 `JFrame` 对象。如果你想使用 `Frame`,你需要将所有的 `JFrame` 替换为 `Frame`。或者,如果你想使用 `JFrame`,你可以将类的继承关系修改为 `public class demo extends JFrame`。
这是一个修正过的代码示例,它应该可以正常工作:
```java
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.*;
public class demo extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();// 创建一个窗口
frame.setTitle("开始你的复制吧!");// 设置窗口标题
frame.setBounds(400, 400, 400, 400);// 设置窗口的位置和大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗口关闭时的操作
frame.setLayout(null);// 不使用布局管理器,设置为NULL
JTextField textField1 = new JTextField(30);
JTextField textField2 = new JTextField(30);
JLabel label1 = new JLabel("输入你要拷贝的文件:");
JLabel label2 = new JLabel("输入拷到哪里去:");
textField1.setColumns(30);
textField2.setColumns(30);
label1.setBounds(10, 10, 120, 20);
label2.setBounds(10, 40, 120, 20);
textField1.setBounds(140, 10, 200, 20);
textField2.setBounds(140, 40, 200, 20);
frame.add(label1);
frame.add(textField1);
frame.add(label2);
frame.add(textField2);
frame.setVisible(true);// 设置Frame为可见,默认不可见
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)