Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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 无法添加JFrame和JTextField_Java_Swing_Paintcomponent_Rect_Border Layout - Fatal编程技术网

Java 无法添加JFrame和JTextField

Java 无法添加JFrame和JTextField,java,swing,paintcomponent,rect,border-layout,Java,Swing,Paintcomponent,Rect,Border Layout,这是我简单代码的一部分,我可以在框架上移动矩形。当我尝试在框架上添加按钮和文本字段时,这些组件不可见或者我看不到矩形。我还尝试先在JPanel上添加它们,然后在框架上添加面板。组件可见,但矩形不可见。有什么建议吗 public class Buffer extends JPanel implements KeyListener,ActionListener{ public static JFrame frame; public static JButton button; public stat

这是我简单代码的一部分,我可以在框架上移动矩形。当我尝试在框架上添加按钮和文本字段时,这些组件不可见或者我看不到矩形。我还尝试先在
JPanel
上添加它们,然后在框架上添加面板。组件可见,但矩形不可见。有什么建议吗

public class Buffer extends JPanel implements KeyListener,ActionListener{
public static JFrame frame;
public static JButton button;
public static JTextField field;
public int x;
public int y;

    public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.red);
    g.fillRect(x,y,20,20);
}

public static void main(String args[]){
    Buffer z=new Buffer();
    frame=new JFrame();
    button=new JButton();
    field=new JTextField(); 
    frame.setLayout(new BorderLayout());

    button.setPreferredSize(new Dimension(20,20));
    button.setText("XY");
    button.addActionListener(z);

    field.setPreferredSize(new Dimension(100,20));
    field.setEditable(false);

    frame.setSize(500,500);
    frame.setVisible(true);
    frame.setFocusable(true);
    frame.addKeyListener(z);
    frame.setTitle("TEST");
}
@Override
public void keyPressed(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_RIGHT){
        x=x+10;
        repaint();
    }
    }
    public void actionPerformed(ActionEvent e){
    field.setText("X- "+x+"       Y- "+y);
    frame.requestFocusInWindow();
}
    }
    }
  • 默认情况下,
    JFrame
    从不对
    KeyEvent
    作出反应,这意味着
    frame.setFocusable(true)

  • 对于
    JPanel
    ,必须
    setFocusable(true)
    ,然后
    KeyListener
    中的
    KeyEvents
    将触发所需事件


  • 不要使用
    Swing JComponent
    ,而是使用(最可伸缩、可设置的)()

  • ,只能看到一个
    JComponent
    ,位于第五个组件之一的位置。区域

      • 默认情况下,
        JFrame
        从不对
        KeyEvent
        作出反应,这意味着
        frame.setFocusable(true)

      • 对于
        JPanel
        ,必须
        setFocusable(true)
        ,然后
        KeyListener
        中的
        KeyEvents
        将触发所需事件


      • 不要使用
        Swing JComponent
        ,而是使用(最可伸缩、可设置的)()

      • ,只能看到一个
        JComponent
        ,位于第五个组件之一的位置。区域


      要将组件添加到框架中,您必须获得框架的容器,然后将组件添加到容器中

      例如,一个示例程序

      import javax.swing.*;
      import java.awt.*;
      
      public class Test
      {
      JFrame f;
      Container c;
      JButton btn;
      JTextField tf;
      public Test() //constructor
      {
      
      f=new JFrame("Swah!");
      f.setBounds(50,50,300,300); //position and dimension of frame
      
      c=f.getContentPane();// getting container of the frame
      c.setLayout(new FlowLayout()); //if you do not use layout then only one 
      //component will be visible to you.
      
      btn=new JButton("OK");
      tf=new JTextField(20);
      
      c.add(btn);
      c.add(tf);
      
      f.setVisible(true);
      f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
      }
      
      public static void main(String []val)
      {
      Test tobj=new Test();
      }
      
      }
      
      您可以根据输出使用布局,有Flowlayout、GridLayout和GridBagLayout等布局


      希望这对您有所帮助。

      要将组件添加到框架中,您必须获得框架的容器,然后将组件添加到容器中

      例如,一个示例程序

      import javax.swing.*;
      import java.awt.*;
      
      public class Test
      {
      JFrame f;
      Container c;
      JButton btn;
      JTextField tf;
      public Test() //constructor
      {
      
      f=new JFrame("Swah!");
      f.setBounds(50,50,300,300); //position and dimension of frame
      
      c=f.getContentPane();// getting container of the frame
      c.setLayout(new FlowLayout()); //if you do not use layout then only one 
      //component will be visible to you.
      
      btn=new JButton("OK");
      tf=new JTextField(20);
      
      c.add(btn);
      c.add(tf);
      
      f.setVisible(true);
      f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
      }
      
      public static void main(String []val)
      {
      Test tobj=new Test();
      }
      
      }
      
      您可以根据输出使用布局,有Flowlayout、GridLayout和GridBagLayout等布局

      希望这对你有帮助