Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTextArea在添加到JPanel时单击之前隐藏_Java_Swing_Jpanel_Jtextarea_Bots - Fatal编程技术网

Java JTextArea在添加到JPanel时单击之前隐藏

Java JTextArea在添加到JPanel时单击之前隐藏,java,swing,jpanel,jtextarea,bots,Java,Swing,Jpanel,Jtextarea,Bots,我在这里试图完成的是在我之前存在的JPanel中添加一个JTextArea。当我运行这个程序时,它实际上会将它添加到我正在寻找的JPanel中,但它会将JTextArea放在JPanel后面,直到我点击它应该在的区域。如果我单击添加到同一JPanel中的另一个JTextArea(与我遇到问题的JPanel相同),那么整个JTextArea将出现 // Once program works as intended, use CardLayout to switch screens, rather

我在这里试图完成的是在我之前存在的JPanel中添加一个JTextArea。当我运行这个程序时,它实际上会将它添加到我正在寻找的JPanel中,但它会将JTextArea放在JPanel后面,直到我点击它应该在的区域。如果我单击添加到同一JPanel中的另一个JTextArea(与我遇到问题的JPanel相同),那么整个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添加滚动窗格?我以前从未使用过JTextAreas,所以我想等程序按预期工作后再添加它

我在这里试图完成的是在我之前存在的JPanel中添加一个JTextArea

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

因此,我所能做的就是发表一般性意见:

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

panel.add( someComponent );
panel.revalidate();
panel.repaint();
2) 不要使用空布局。Swing设计用于布局管理器,原因太多,无法在此列出

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

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

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

也许可以从上的Swing教程开始,通过演示了解Swing的基本信息,这将向您展示如何更好地构造代码

我在这里试图完成的是在我之前存在的JPanel中添加一个JTextArea

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

因此,我所能做的就是发表一般性意见:

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

panel.add( someComponent );
panel.revalidate();
panel.repaint();
2) 不要使用空布局。Swing设计用于布局管理器,原因太多,无法在此列出

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

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

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

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

  • 屏幕设置为可见(true)
    并将其放在
    运行的末尾
    。如果需要动态更新UI,则需要使用
    revalidate
    后跟
    repaint
    来指示UI更新其布局信息。如果可能,请使用
    CardLayout
    在多个视图之间动态切换,有关详细信息,请参阅
  • 避免使用
    null
    布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题
      • 屏幕设置为可见(true)
        并将其放在
        运行的末尾
        。如果需要动态更新UI,则需要使用
        revalidate
        后跟
        repaint
        来指示UI更新其布局信息。如果可能,请使用
        CardLayout
        在多个视图之间动态切换,有关详细信息,请参阅
      • 避免使用
        null
        布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题

      很抱歉,下次我会说得更具体一些。没有想过尝试.repaint(),但这就是它所需要的全部。谢谢对不起,下次我会说得更具体一些。没有想过尝试.repaint(),但这就是它所需要的全部。谢谢