java 如何让下一个按钮进入下一帧?图形用户界面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15921854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:18:02  来源:igfitidea点击:

How do i make next button go to next Frame? GUI

javauser-interface

提问by WB4991

How do I make the next button go to the next Frame in this GUI? I need to have it where I can click next to display 20 more details:

如何使下一个按钮转到此 GUI 中的下一个框架?我需要把它放在我可以点击下一步以显示 20 个更多细节的地方:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FilledFrameViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();

      /*JButton button = new JButton*/
      JButton nextButton = new JButton("NEXT");
      JLabel label = new JLabel("Frame 1.");

      JPanel panel = new JPanel();
      panel.add(nextButton);
      panel.add(label);
      frame.add(panel);

      final int FRAME_WIDTH = 300;
      final int FRAME_HEIGHT = 100;
      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setTitle("A frame with two components");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.setVisible(true);
   }
}

回答by Annu

If you want to hide or show the frame, you can use like this

如果你想隐藏或显示框架,你可以这样使用

JFrame f1 = new JFrame("Frame 1"); JFrame f2 = new JFrame("Frame 2");

JFrame f1 = new JFrame("第一帧"); JFrame f2 = new JFrame("Frame 2");

to hide f1, call f1.setVisible(false); to show f2, call f2.setVisible(true);

隐藏 f1,调用 f1.setVisible(false); 要显示 f2,请调用 f2.setVisible(true);

回答by Lurvas777

A bit unclear. Do you want to move a JButtonto another JFrame? I don't think you can manage that without dynamic programming or such (I guess?).

有点不清楚。您想将JButton移动到另一个JFrame吗?我认为如果没有动态编程之类的东西(我猜?),你就无法做到这一点。

You should look through a tutorial of the Swing components from Oracle http://docs.oracle.com/javase/tutorial/uiswing/

您应该浏览 Oracle http://docs.oracle.com/javase/tutorial/uiswing/的 Swing 组件教程

And see your comments. I agree having multiple JFrameis a bad habit.

并查看您的评论。我同意拥有多个JFrame是一个坏习惯。

Edit

编辑

Use the eventlistner (see other answer) and make it follow a switch case? Then there you can make it display like a dialog or change a JLableinside your JFrame?

使用 eventlistner(请参阅其他答案)并使其遵循 switch case?然后你可以让它像对话框一样显示或在你的JFrame 中更改JLable

Dialog tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html. I think you should go with a lable and change it's text.

此处的对话框教程:http: //docs.oracle.com/javase/tutorial/uiswing/components/dialog.html。我认为您应该使用标签并更改其文字。

JButton nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListner(

private int counter = 0;

public void actionPerformed(ActionEvent e) { 

    counter++;

    switch(counter){

        case 1: somelable.setText("Your text here");

    };

}));

I wrote this on free hand but I guess something like this would work?

我是徒手写的,但我想这样的事情会奏效吗?

回答by NH Narumi

You have to edit the codes in your next frame... Example for your NextFrame,

您必须在下一帧中编辑代码... NextFrame 示例,

public class NextFrame
   public static void main(String[] args)
   {
      private static FilledFrameViewer parentFrame; //ADD THIS FOR CONNECTION TO FIRST FRAME

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NextFrame frame = new NextFrame(null); //CHANGES MADE HERE
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

      public NextFrame(FilledFrameViewer f) { //CHANGES MADE HERE
      this.parentFrame = f; //CHANGES MADE HERE
        setTitle("Personal Assistant");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

回答by NH Narumi

Ah, Yes...and also you'll have to add some things in the Next button...

啊,是的……而且您还必须在“下一步”按钮中添加一些内容……

Example:

例子:

 btnNext = new JButton();
    btnNext.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            goNext();
            {
                private void goNext(){
                                    NextFrame nextframe= new NextFrame(null);
                                    nextframe.setVisible(true);
            }
            }

    });