【发布时间】:2016-01-13 09:29:01
【问题描述】:
我必须创建一个程序,在该程序中,我将在对话框中向用户显示一些选项以供选择。 根据用户选择的选项,我必须在另一个之前为空的对话框中显示该图片。
例子:
- 对话框“一”和“二”对用户都是可见的。对话框“一个”上显示了许多按钮。对话框“二”是空的。
- 用户单击对话框“一”上可用的按钮 A,然后我必须在对话框“二”上显示该图片。
- 用户单击对话框“一”上可用的不同按钮 B,然后我必须在对话框“二”上显示该图片以及旧图片。
这是否可以在不创建新对话框“two”或不为对话框“two”创建新 JPanel 的情况下动态完成。
到目前为止,我已经创建了下面的程序,但它运行后没有添加图片。
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.plaf.metal.MetalIconFactory.FolderIcon16;
public class Launcher {
JDialog keyboardDialog;
JDialog nameViewDialog;
JPanel nameViewJPanel;
JDialog FinalNameViewDialog;
private final transient ActionListener keyButtonListener =
new ActionListener() {
@Override public void actionPerformed(final ActionEvent event) {
System.out.println( ((JButton) event.getSource()).getActionCommand());
String buttonType=((JButton) event.getSource()).getActionCommand();
ImageIcon iconA = new ImageIcon(this.getClass().getResource("\\Icons\\A1.PNG"));
JLabel la=new JLabel(iconA);
nameViewJPanel.add(la);
nameViewJPanel.repaint();
}
};
public Launcher()
{
nameViewDialog=new JDialog();
nameViewDialog.setLayout(new BorderLayout());
nameViewJPanel=new JPanel();
nameViewJPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
nameViewDialog.setSize(430, 490);
ImageIcon iconA1 = new ImageIcon(this.getClass().getResource("\\Icons\\A1.PNG"));
JLabel la=new JLabel(iconA1);
nameViewJPanel.add(la);
ImageIcon iconA2 = new ImageIcon(this.getClass().getResource("\\Icons\\B1.PNG"));
JLabel lb=new JLabel(iconA2);
nameViewJPanel.add(lb);
nameViewDialog.add(nameViewJPanel);
keyboardDialog=new JDialog(nameViewDialog,ModalityType.MODELESS);
keyboardDialog.setLocationRelativeTo(nameViewDialog);
keyboardDialog.setSize(230,190);
keyboardDialog.setLayout(new GridLayout(2,3));
ImageIcon iconA = new ImageIcon(this.getClass().getResource("\\JaLetters\\A.PNG"));
ImageIcon iconB = new ImageIcon(this.getClass().getResource("\\JaLetters\\B.PNG"));
ImageIcon iconC = new ImageIcon(this.getClass().getResource("\\JaLetters\\C.PNG"));
ImageIcon iconD = new ImageIcon(this.getClass().getResource("\\JaLetters\\D.PNG"));
ImageIcon iconE = new ImageIcon(this.getClass().getResource("\\JaLetters\\E.PNG"));
ImageIcon iconF = new ImageIcon(this.getClass().getResource("\\JaLetters\\F.PNG"));
JButton ba=new JButton();
ba.setIcon(iconA);
ba.setActionCommand("A");
ba.addActionListener(keyButtonListener);
JButton bb=new JButton();
bb.setIcon(iconB);
bb.setActionCommand("B");
bb.addActionListener(keyButtonListener);
JButton bc=new JButton();
bc.setIcon(iconC);
bc.setActionCommand("C");
bc.addActionListener(keyButtonListener);
JButton bd=new JButton();
bd.setIcon(iconD);
bd.setActionCommand("D");
bd.addActionListener(keyButtonListener);
JButton be=new JButton();
be.setIcon(iconE);
be.setActionCommand("E");
be.addActionListener(keyButtonListener);
JButton bf=new JButton();
bf.setIcon(iconF);
bf.setActionCommand("F");
bf.addActionListener(keyButtonListener);
keyboardDialog.add(ba);
keyboardDialog.add(bb);
keyboardDialog.add(bc);
keyboardDialog.add(bd);
keyboardDialog.add(be);
keyboardDialog.add(bf);
nameViewDialog.setVisible(true);
keyboardDialog.setVisible(true);
}
public static void main(String args[])
{
new Launcher();
}
}
【问题讨论】:
-
这个尖叫模型和Observer Pattern。但是,我会犹豫是否使用两个对话框来执行此操作,可能更喜欢使用
JSplitPane中的面板或使用第二个对话框作为弹出窗口来收集信息,当关闭时,允许第一个窗口自行更新