java - JTextArea 在添加到 JPanel 时隐藏,直到单击为止

标签 java swing jpanel jtextarea bots

我在这里想要完成的是将 JTextArea 添加到我预先存在的 JPanel 中。当我运行该程序时,它实际上将其添加到我正在查找的 JPanel 中,但它将 JTextArea 放在 JPanel 后面,直到我单击它应该所在的区域。如果我单击添加到与我遇到问题的 JPanel 相同的 JTextArea 的另一个 JTextArea,则会显示整个 JTextArea。

 // Once program works as intended, use CardLayout to switch screens, rather than using JFrame with setVisible(true/false)
 // JPanels. Tutorial left on Stackoverflow with example code. 12/31/15


 import java.awt.BorderLayout;
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.*;

 public class UserInterface implements ActionListener 
 {

 JFrame screen; 
 JButton start, submit, help;
 JPanel UI,userWhite, passWhite;
 JLabel usrTxt, passTxt;
 JTextArea usrInput, passInput, console;

static String[] strings = new String[2]; //See getInformation() method
HELP runner = new HELP();

public static void main(String args[])
{
    UserInterface userinterface = new UserInterface();
    userinterface.uiSetup();
    userinterface.displaySetup();
    userinterface.inputSetup();
    userinterface.consoleSetup();
}

public void run()
{
    uiSetup();
    displaySetup();
    inputSetup();
    consoleSetup();
}


public void uiSetup()
{
    // This method sets up the initial interface which all other
    // elements within this program will be built off of

    screen = new JFrame("Bing Rewards Bot v.Development 1.0");
    screen.setVisible(true);
    screen.setSize(800, 600);
    screen.setResizable(false);
    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    screen.getContentPane().setLayout(new BorderLayout());

    UI = new JPanel();
    UI.setVisible(true);
    UI.setLayout(null);
    UI.setBackground(Color.CYAN);
    screen.add(UI);
                                        // Buttons must me initialized here. If done in another method,
                                        // it can sometimes block JPanels from appearing. Fixed: 12/27/15
    start = new JButton("Start Bot");
    start.setVisible(true);
    start.setFocusable(false);
    start.setBounds(150,500,200,75);
    UI.add(start);

    submit = new JButton("Submit");
    submit.setVisible(true);
    submit.setFocusable(false);
    submit.setBounds(75,170,100,50);
    UI.add(submit);

    help = new JButton("HELP");
    help.setVisible(true);
    help.setFocusable(false);
    help.setBounds(455,500,200,75);
    help.setActionCommand("helpClicked");
    help.addActionListener(this);
    UI.add(help); 
}


public void displaySetup()
{
    // This method sets up the interface text

    usrTxt = new JLabel("Bing/Microsoft Account Username");
    usrTxt.setFont(new Font("Monospaced", Font.BOLD, 16));
    usrTxt.setForeground(Color.BLACK);
    usrTxt.setBounds(0,0,310,20); // Adjusts Absolute Size
    usrTxt.setLocation(25,50); // Sets Location
    UI.add(usrTxt);

    passTxt = new JLabel("Password");
    passTxt.setFont(new Font("Monospaced", Font.BOLD, 16));
    passTxt.setForeground(Color.BLACK);
    passTxt.setBounds(0,0,310,20);
    passTxt.setLocation(25,100);
    UI.add(passTxt);
}

public void consoleSetup()
{
    //TODO Appearing behind JPanel: UI

    console = new JTextArea("Bing Rewards Bot v.Development 1.1\nInterface Loaded Sucessfully. Awaiting User Input...");
    console.setSize(700,200);
    console.setLocation(25,260);
    console.setBackground(Color.WHITE);
    console.setForeground(Color.BLACK);
    console.setVisible(true);
    console.setEditable(false);
    // console.setText("01/02/16//:: Initalizing JTextArea()");
    UI.add(console);

}

public void inputSetup()
{
    // This method handles the User name and Password field setup


    userWhite = new JPanel();
    userWhite.setVisible(true);
    userWhite.setBackground(Color.WHITE);
    userWhite.setLocation(25,70);
    userWhite.setSize(200,25);
    UI.add(userWhite);

    passWhite = new JPanel();
    passWhite.setVisible(true);
    passWhite.setBackground(Color.WHITE);
    passWhite.setLocation(25,120);
    passWhite.setSize(200,25);
    UI.add(passWhite);

    usrInput = new JTextArea();
    usrInput.setBounds(0,0,200,18);
    usrInput.setLocation(25,75);
    usrInput.setBackground(Color.WHITE);
    usrInput.setForeground(Color.BLACK);
    UI.add(usrInput);

    passInput = new JTextArea();
    passInput.setBounds(0,0,200,18);
    passInput.setLocation(25,125);
    passInput.setBackground(Color.WHITE);
    passInput.setForeground(Color.BLACK);
    UI.add(passInput);

}

public void getInformation()
{
    // This method gets the information from the
    // JTextAreas in the inputSetup method and
    // stores it into an array called "strings"
    // ("strings" is initialized in the class)

    strings[0] = usrInput.getText();
    strings[1] = passInput.getText();
}

public void actionPerformed(ActionEvent e){
    if (e.getActionCommand().equals("helpClicked")) 
    {
        UI.setVisible(false);
        runner.helpSetup(screen);

    }
}

另外,是否需要向 JTextArea 添加滚动 Pane ?我以前从未使用过 JTextAreas,所以我想一旦程序按预期工作,我就会添加它。

最佳答案

What I'm trying to accomplish here is adding a JTextArea to my pre-existing JPanel

嗯,我不知道你在谈论代码的哪一部分。我们没有时间阅读您的整个代码来理解逻辑。

所以我所能做的就是发表一般性评论:

1) GUI可见后添加组件的基本代码是:

panel.add( someComponent );
panel.revalidate();
panel.repaint();

2) 不要使用空布局。 Swing 被设计为与布局管理器一起使用,原因太多,无法在此处列出。

3) 变量名称不应以大写字符开头。你们中的大多数人的名字都是正确的,但也有一些不正确。保持一致!!!

4) 在使框架可见之前,应将组件添加到框架中。

5) GUI 应在事件调度线程 (EDT) 上创建。

也许可以从 Layout Manager 上的 Swing 教程开始了解基本的 Swing 信息以及工作演示,这些演示将向您展示如何更好地构建代码。

关于java - JTextArea 在添加到 JPanel 时隐藏,直到单击为止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34572065/

相关文章:

java - 如何将 JPanel 添加到不同类的容器中?

java - 如果我使用spring data Rest+spring data jpa,是否能保证service层和Dao层的代码为零,需要手动编码?

java - 将 Swift 数据转换为 Int16

java - 如何序列化JPanel?

Java JFrame 对话框问题

java - 设置 JPanel 的大小不依赖于 JFrame 的大小

java - 如何更改实际复选框内 J 复选框的背景颜色

java - 当字符串已经被初始化和分配时拒绝声明

java 尝试在通知中改变

java - 使用按钮关闭 JFrame