/**
@version 1.3 2000-05-01
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;public class OptionDialogTest
{
public static void main(String[] args)
{
OptionDialogFrame frame = new OptionDialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
A panel with radio buttons inside a titled border.
*/
class ButtonPanel extends JPanel
{
/**
Constructs a button panel.
@param title the title shown in the border
@param options an array of radio button labels
*/
public ButtonPanel(String title, String[] options)
{
setBorder(BorderFactory.createTitledBorder
(BorderFactory.createEtchedBorder(), title));
setLayout(new BoxLayout(this,
BoxLayout.Y_AXIS));
group = new ButtonGroup();
// make one radio button for each option
for (int i = 0; i < options.length; i++)
{
JRadioButton b = new JRadioButton(options[i]);
b.setActionCommand(options[i]);
add(b);
group.add(b);
b.setSelected(i == 0);
}
} /**
Gets the currently selected option.
@return the label of the currently selected radio button.
*/
public String getSelection()
{
return group.getSelection().getActionCommand();
}
private ButtonGroup group;
}
/**
A frame that contains settings for selecting various option
dialogs.
*/
class OptionDialogFrame extends JFrame
{
public OptionDialogFrame()
{
setTitle("OptionDialogTest");
setSize(WIDTH, HEIGHT); JPanel gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(2, 3));
@version 1.3 2000-05-01
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;public class OptionDialogTest
{
public static void main(String[] args)
{
OptionDialogFrame frame = new OptionDialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
A panel with radio buttons inside a titled border.
*/
class ButtonPanel extends JPanel
{
/**
Constructs a button panel.
@param title the title shown in the border
@param options an array of radio button labels
*/
public ButtonPanel(String title, String[] options)
{
setBorder(BorderFactory.createTitledBorder
(BorderFactory.createEtchedBorder(), title));
setLayout(new BoxLayout(this,
BoxLayout.Y_AXIS));
group = new ButtonGroup();
// make one radio button for each option
for (int i = 0; i < options.length; i++)
{
JRadioButton b = new JRadioButton(options[i]);
b.setActionCommand(options[i]);
add(b);
group.add(b);
b.setSelected(i == 0);
}
} /**
Gets the currently selected option.
@return the label of the currently selected radio button.
*/
public String getSelection()
{
return group.getSelection().getActionCommand();
}
private ButtonGroup group;
}
/**
A frame that contains settings for selecting various option
dialogs.
*/
class OptionDialogFrame extends JFrame
{
public OptionDialogFrame()
{
setTitle("OptionDialogTest");
setSize(WIDTH, HEIGHT); JPanel gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(2, 3));
解决方案 »
- 对象序列化与反序列化中一些问题
- Spring集成RMI实现客户访问服务器,如何实现冗余服务器操作?
- 程序帮忙给该一下!
- jdbc更新停顿问题。orcale9i,使用ojdbc14.jar
- 如何在sun solaris8上安装jre或jdk?我安装老是出问题!
- 一个有参数的方法,我用什么一保存这些参数呢
- 有谁了解上海和为科技有限公司 !正在找工作中,多谢
- 新手求救,环境配置问题(急!)
- actionPerformed为什么在我点击connect按钮的时候不响应阿?大虾高手帮忙阿?
- JAVA如何判断浏览器的类型,如何存取COOKIE的值,请给个源代码!!!
- 请教JTable和JTableModel的问题
- 为什么我使用jbuilder7加载对mysql驱动后仍然无法访问数据库中的字段啊!!!高分送英雄
@version 1.3 2000-05-01
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;public class OptionDialogTest
{
public static void main(String[] args)
{
OptionDialogFrame frame = new OptionDialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
A panel with radio buttons inside a titled border.
*/
class ButtonPanel extends JPanel
{
/**
Constructs a button panel.
@param title the title shown in the border
@param options an array of radio button labels
*/
public ButtonPanel(String title, String[] options)
{
setBorder(BorderFactory.createTitledBorder
(BorderFactory.createEtchedBorder(), title));
setLayout(new BoxLayout(this,
BoxLayout.Y_AXIS));
group = new ButtonGroup();
// make one radio button for each option
for (int i = 0; i < options.length; i++)
{
JRadioButton b = new JRadioButton(options[i]);
b.setActionCommand(options[i]);
add(b);
group.add(b);
b.setSelected(i == 0);
}
} /**
Gets the currently selected option.
@return the label of the currently selected radio button.
*/
public String getSelection()
{
return group.getSelection().getActionCommand();
}
private ButtonGroup group;
}
/**
A frame that contains settings for selecting various option
dialogs.
*/
class OptionDialogFrame extends JFrame
{
public OptionDialogFrame()
{
setTitle("OptionDialogTest");
setSize(WIDTH, HEIGHT); JPanel gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(2, 3));
typePanel = new ButtonPanel("Type",
new String[]
{
"Message",
"Confirm",
"Option",
"Input"
}); messageTypePanel = new ButtonPanel("Message Type",
new String[]
{
"ERROR_MESSAGE",
"INFORMATION_MESSAGE",
"WARNING_MESSAGE",
"QUESTION_MESSAGE",
"PLAIN_MESSAGE"
});
messagePanel = new ButtonPanel("Message",
new String[]
{
"String",
"Icon",
"Component",
"Other",
"Object[]"
});
optionTypePanel = new ButtonPanel("Confirm",
new String[]
{
"DEFAULT_OPTION",
"YES_NO_OPTION",
"YES_NO_CANCEL_OPTION",
"OK_CANCEL_OPTION"
});
optionsPanel = new ButtonPanel("Option",
new String[]
{
"String[]",
"Icon[]",
"Object[]"
});
inputPanel = new ButtonPanel("Input",
new String[]
{
"Text field",
"Combo box"
});
gridPanel.add(typePanel);
gridPanel.add(messageTypePanel);
gridPanel.add(messagePanel);
gridPanel.add(optionTypePanel);
gridPanel.add(optionsPanel);
gridPanel.add(inputPanel); // add a panel with a Show button JPanel showPanel = new JPanel();
JButton showButton = new JButton("Show");
showButton.addActionListener(new ShowAction());
showPanel.add(showButton);
Container contentPane = getContentPane();
contentPane.add(gridPanel, BorderLayout.CENTER);
contentPane.add(showPanel, BorderLayout.SOUTH);
} /**
Gets the currently selected message.
@return a string, icon, component or object array,
depending on the Message panel selection
*/
public Object getMessage()
{
String s = messagePanel.getSelection();
if (s.equals("String"))
return messageString;
else if (s.equals("Icon"))
return messageIcon;
else if (s.equals("Component"))
return messageComponent;
else if (s.equals("Object[]"))
return new Object[]
{
messageString,
messageIcon,
messageComponent,
messageObject
};
else if (s.equals("Other"))
return messageObject;
else return null;
}
/**
Gets the currently selected options.
@return an array of strings, icons or objects, depending
on the Option panel selection
*/
public Object[] getOptions()
{
String s = optionsPanel.getSelection();
if (s.equals("String[]"))
return new String[] { "Yellow", "Blue", "Red" };
else if (s.equals("Icon[]"))
return new Icon[]
{
new ImageIcon("yellow-ball.gif"),
new ImageIcon("blue-ball.gif"),
new ImageIcon("red-ball.gif")
};
else if (s.equals("Object[]"))
return new Object[]
{
messageString,
messageIcon,
messageComponent,
messageObject
};
else
return null;
} /**
Gets the selected message or option type
@param panel the Message Type or Confirm panel
@return the selected XXX_MESSAGE or XXX_OPTION constant
from the JOptionPane class
*/
public int getType(ButtonPanel panel)
{
String s = panel.getSelection();
try
{
return JOptionPane.class.getField(s).getInt(null);
}
catch(Exception e)
{
return -1;
}
} /**
The action listener for the Show button shows a
Confirm, Input, Message or Option dialog depending
on the Type panel selection.
*/
private class ShowAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
if (typePanel.getSelection().equals("Confirm"))
JOptionPane.showConfirmDialog(
OptionDialogFrame.this,
getMessage(),
"Title",
getType(optionTypePanel),
getType(messageTypePanel));
else if (typePanel.getSelection().equals("Input"))
{
if (inputPanel.getSelection().equals("Text field"))
JOptionPane.showInputDialog(
OptionDialogFrame.this,
getMessage(),
"Title",
getType(messageTypePanel));
else
JOptionPane.showInputDialog(
OptionDialogFrame.this,
getMessage(),
"Title",
getType(messageTypePanel),
null,
new String[] { "Yellow", "Blue", "Red" },
"Blue");
}
else if (typePanel.getSelection().equals("Message"))
JOptionPane.showMessageDialog(
OptionDialogFrame.this,
getMessage(),
"Title",
getType(messageTypePanel));
else if (typePanel.getSelection().equals("Option"))
JOptionPane.showOptionDialog(
OptionDialogFrame.this,
getMessage(),
"Title",
getType(optionTypePanel),
getType(messageTypePanel),
null,
getOptions(),
getOptions()[0]);
}
} public static final int WIDTH = 600;
public static final int HEIGHT = 400; private ButtonPanel typePanel;
private ButtonPanel messagePanel;
private ButtonPanel messageTypePanel;
private ButtonPanel optionTypePanel;
private ButtonPanel optionsPanel;
private ButtonPanel inputPanel;
private String messageString = "Message";
private Icon messageIcon = new ImageIcon("blue-ball.gif");
private Object messageObject = new Date();
private Component messageComponent = new SamplePanel();
}/**
A panel with a painted surface
*/class SamplePanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Rectangle2D rect = new Rectangle2D.Double(0, 0,
getWidth() - 1, getHeight() - 1);
g2.setPaint(Color.yellow);
g2.fill(rect);
g2.setPaint(Color.blue);
g2.draw(rect);
} public Dimension getMinimumSize()
{
return new Dimension(10, 10);
}
}
/**
@version 1.3 2000-05-01
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class DataExchangeTest
{
public static void main(String[] args)
{
DataExchangeFrame frame = new DataExchangeFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
A frame with a menu whose File->Connect action shows a
password dialog.
*/
class DataExchangeFrame extends JFrame
{
public DataExchangeFrame()
{
setTitle("DataExchangeTest");
setSize(WIDTH, HEIGHT); // construct a File menu
JMenuBar mbar = new JMenuBar();
setJMenuBar(mbar);
JMenu fileMenu = new JMenu("File");
mbar.add(fileMenu); // add Connect and Exit menu items JMenuItem connectItem = new JMenuItem("Connect");
connectItem.addActionListener(new ConnectAction());
fileMenu.add(connectItem); // The Exit item exits the program JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
fileMenu.add(exitItem); textArea = new JTextArea();
getContentPane().add(new JScrollPane(textArea),
BorderLayout.CENTER);
} public static final int WIDTH = 300;
public static final int HEIGHT = 200; private PasswordChooser dialog = null;
private JTextArea textArea; /**
The Connect action pops up the password dialog.
*/ private class ConnectAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// if first time, construct dialog if (dialog == null)
dialog = new PasswordChooser();
// set default values
dialog.setUser(new User("yourname", null)); // pop up dialog
if (dialog.showDialog(DataExchangeFrame.this,
"Connect"))
{
// if accepted, retrieve user input
User u = dialog.getUser();
textArea.append(
"user name = " + u.getName()
+ ", password = " + (new String(u.getPassword()))
+ "\n");
}
}
}
}/**
A password chooser that is shown inside a dialog
*/
class PasswordChooser extends JPanel
{
public PasswordChooser()
{
setLayout(new BorderLayout()); // construct a panel with user name and password fields JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 2));
panel.add(new JLabel("User name:"));
panel.add(username = new JTextField(""));
panel.add(new JLabel("Password:"));
panel.add(password = new JPasswordField(""));
add(panel, BorderLayout.CENTER); // create Ok and Cancel buttons that terminate the dialog
JButton okButton = new JButton("Ok");
okButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
ok = true;
dialog.setVisible(false);
}
}); JButton cancelButton = new JButton("Cancel");
okButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
dialog.setVisible(false);
}
}); // add buttons to southern border JPanel buttonPanel = new JPanel();
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
add(buttonPanel, BorderLayout.SOUTH);
} /**
Sets the dialog defaults.
@param u the default user information
*/
public void setUser(User u)
{
username.setText(u.getName());
} /**
Gets the dialog entries.
@return a User object whose state represents
the dialog entries
*/
public User getUser()
{
return new User(username.getText(),
password.getPassword());
} /**
Show the chooser panel in a dialog
@param parent a component in the owner frame or null
@param title the dialog window title
*/
public boolean showDialog(Component parent, String title)
{
ok = false; // locate the owner frame Frame owner = null;
if (parent instanceof Frame)
owner = (Frame) parent;
else
owner = (Frame)SwingUtilities.getAncestorOfClass(
Frame.class, parent); // if first time, or if owner has changed, make new dialog
if (dialog == null || dialog.getOwner() != owner)
{
owner = null;
dialog = new JDialog(owner, true);
dialog.getContentPane().add(this);
dialog.pack();
} // set title and show dialog dialog.setTitle(title);
dialog.show();
return ok;
} private JTextField username;
private JPasswordField password;
private boolean ok;
private JDialog dialog;
}/**
A user has a name and password. For security reasons, the
password is stored as a char[], not a String.
*/
class User
{
public User(String aName, char[] aPassword)
{
name = aName;
password = aPassword;
}
public String getName() { return name; }
public char[] getPassword() { return password; } public void setName(String aName) { name = aName; }
public void setPassword(char[] aPassword)
{ password = aPassword; } private String name;
private char[] password;
}