java中的GUI“转到上一个/下一个”选项? [英] GUI in java "go to previous/next" option?

查看:160
本文介绍了java中的GUI“转到上一个/下一个”选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法有一个选项去上一个消息对话框或下一个消息对话框?我有一个程序,所有的输入和数学计算完成后,出现一个消息对话框,其中包含Person 1的信息,然后按ok,并显示Person 2。如果可以选择在不同的对话框之间导航,这将是很好的。这是打印消息的程序的一部分。

  for(i = 0; i< NumEmployees; i ++)
{
JOptionPane.showMessageDialog(null,
Employee:+ names [i] +\\\
+
ID:+ data [i] [0] +\\\
+
工作时间:+(data [i] [1] + data [i] [2])+\\\
+
加班:+数据[i] [2] +小时+\\\
+
赚取的金额:+付款[i]);
}


解决方案

使用 Action 分开功能和状态。在下面的示例中,操作会更改索引 update() a JLabel 列表< String> 。您的应用程序可能会从列表< Employee> 更新 JTextArea



  package gui; 

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/ **
* @see http://stackoverflow.com/a/20116944/230513
* /
public class PrevNext {

private final列表< String> list = new ArrayList(
Arrays.asList(Alpher,Bethe,Gamow,Dirac,Einstein));
private int index = list.indexOf(Einstein);
private final JLabel label = new JLabel(list.get(index),JLabel.CENTER);

private void display(){
JFrame f = new JFrame(PrevNext);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JButton(new AbstractAction(< Prev){

@Override
public void actionPerformed(ActionEvent e){
if( - -index <0){
index = list.size() - 1;
}
update();
}
}),BorderLayout.LINE_START) ;
f.add(label);
f.add(new JButton(new AbstractAction(Next>){

@Override
public void actionPerformed(ActionEvent e){
if(++ index == list.size()){
index = 0;
}
update();
}
}),BorderLayout.LINE_END);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

private void update(){
label.setText(list.get(index));
}

public static void main(String [] args){
EventQueue.invokeLater(new Runnable(){

@Override
public void run(){
new PrevNext()。display();
}
});
}
}


Is there a way to have an option to go the a previous message dialog box or a next one? I have a program where after all the input and math calculations is done, a message dialog box appears with the information for "Person 1" then you press ok and the one for "Person 2" appears. It would be nice if there could be an option to be able to navigate between the different dialog boxes. Here is the part of the program that prints the messages.

for (i = 0; i < NumEmployees; i++)    
{
    JOptionPane.showMessageDialog(null, 
            "Employee: " + names[i] + "\n" + 
            "ID: " + data[i][0] + "\n" + 
            "Hours worked: " + (data[i][1] + data[i][2]) + "\n" + 
            "Overtime: " + data[i][2] + "hours" + "\n" + 
            "Amount earned: " + payment[i]);    
}

解决方案

Use Action "to separate functionality and state from a component." In the example below, the actions change the index and update() a JLabel from a List<String>. Your application might update a JTextArea from a List<Employee>.

package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * @see http://stackoverflow.com/a/20116944/230513
 */
public class PrevNext {

    private final List<String> list = new ArrayList<>(
        Arrays.asList("Alpher", "Bethe", "Gamow", "Dirac", "Einstein"));
    private int index = list.indexOf("Einstein");
    private final JLabel label = new JLabel(list.get(index), JLabel.CENTER);

    private void display() {
        JFrame f = new JFrame("PrevNext");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JButton(new AbstractAction("<Prev") {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (--index < 0) {
                    index = list.size() - 1;
                }
                update();
            }
        }), BorderLayout.LINE_START);
        f.add(label);
        f.add(new JButton(new AbstractAction("Next>") {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (++index == list.size()) {
                    index = 0;
                }
                update();
            }
        }), BorderLayout.LINE_END);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private void update() {
        label.setText(list.get(index));
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new PrevNext().display();
            }
        });
    }
}

这篇关于java中的GUI“转到上一个/下一个”选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆