Nob*_*fer 1 java user-interface swing jtextarea
我正在尝试为我正在编写的 Java 应用程序创建一个简单的 swing GUI,但是在将内容显示在 JPanel 上时遇到了一些麻烦,我想知道是否有人可以指出我在做什么错误的?
我的 Gui.java 类中有以下代码:
package openDIS;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Gui extends JFrame{
public Gui(){
setTitle("DIS Filter");
setSize(1000, 500);
setLocation (10, 10);
setDefaultCloseOperation(EXIT_ON_CLOSE);
initGui();
}
/*public quitButton(){
initGui();
} */
private void initGui(){
//JFrame frame = new JFrame();
JPanel panel = new JPanel();
this.getContentPane().add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("DIS Filter");
this.setSize(1000, 500);
panel.setLayout(null);
/*Add a JTextArea to display the output DIS information */
JTextArea displayOutput = new JTextArea();
panel.add(displayOutput);
//String data = EspduReceiver.espdu;
int n = EspduReceiver.entitySite.size();
for (int i = 0; i < n; i++){
EspduReceiver.receivePdu();
System.out.println(EspduReceiver.entitySite.get(i));
System.out.println(EspduReceiver.entityApplication.get(i));
System.out.println(EspduReceiver.entity.get(i));
displayOutput.append(EspduReceiver.entitySite.get(i).toString());
displayOutput.append(EspduReceiver.entityApplication.get(i).toString());
displayOutput.append(EspduReceiver.entity.get(i).toString());
}
JButton quitButton = new JButton("Quit");
panel.add(quitButton);
quitButton.setBounds(875, 400, 80, 30); /*Set the location of the button in the window, and its size */
quitButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
panel.add(quitButton);
//setTitle("Quit");
//setSize(60,30); /*This line was overwriting the previously set values for the size of the window */
setLocationRelativeTo(null);
panel.repaint();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args){ /* I probably don't need a main method here- I have one in EspduReceiver.java */
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
Gui gui = new Gui();
gui.setVisible(true);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
目前,当我运行该课程时,会打开一个标题为“DIS Filter”的窗口,并在右下角有一个“退出”按钮 - 退出按钮功能正常。
但是,无论我尝试什么,我似乎都无法显示 JTextArea...任何人都可以指出我在这里做错了什么吗?
谢谢!
您仍然需要一个布局,使用 FlowLayout 作为最简单的布局。您还必须设置 JTextArea 大小。
panel.setLayout(new FlowLayout());
/* Add a JTextArea to display the output DIS information */
JTextArea displayOutput = new JTextArea(50, 50);
panel.add(displayOutput);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8218 次 |
| 最近记录: |