【发布时间】:2016-06-07 00:40:42
【问题描述】:
我已经在互联网上搜索了解决方案,但似乎没有找到适合我的解决方案...如果您想知道,我是 Swing 的新手。所以,事情是这样的,JButton 出现了,但 JTextArea 没有出现。我不知道该怎么做才能解决这个问题...帮帮我...
public class FrameCreation
{
public JFrame createFrame(int width, int height, String name)
{
JFrame frame = new JFrame(name);
frame.setVisible(true);
frame.setSize(width, height);
frame.setLayout(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.DARK_GRAY);
return frame;
}
public JButton createButton(int width, int height, int xPos, int yPos, String text)
{
JButton button = new JButton(text);
button.setBounds(xPos, yPos, width, height);
button.setBackground(Color.GRAY);
button.setForeground(Color.WHITE);
return button;
}
public JTextArea createTextArea(int width, int height, int xPos, int yPos)
{
JTextArea txt = new JTextArea();
txt.setVisible(true);
txt.setBounds(xPos, yPos, width, height);
txt.setText("Help this poor JTextArea to appear on the frame...");
return txt;
}
}
public class Main
{
public static void main(String[] args)
{
FrameCreation mainFrame = new FrameCreation();
JFrame f = new FrameCreation().createFrame(600, 600, "My Frame");
f.add(mainFrame.createButton(100, 40, 10, 10, "Click me!"));
f.add(mainFrame.createTextArea(200, 200, 390, 10));
}
}
【问题讨论】:
-
用实际代码和输出编辑了我的答案;现在它正在工作:)
标签: java swing jframe jbutton jtextarea