IO操作你学过没??学过的话,就用IO操作把文本内容读出来,然后调用你那个文本框的什么setText方法吧,放进去就OK了。
站在用户的角度思考问题,与客户深入沟通,找到文圣网站设计与文圣网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广、域名注册、网页空间、企业邮箱。业务覆盖文圣地区。
我帮你编了个最简单的例子。代码中已经标志了关键性代码。你可以运行看看的^_^ import java.awt.BorderLayout; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JToolBar; import java.awt.Rectangle; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; public class MyItem extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JMenuBar jJMenuBar = null; private JMenu jMenu = null; private JMenuItem jMenuItem = null; /** * This is the default constructor */ public MyItem() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(300, 200); this.setJMenuBar(getJJMenuBar()); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); } return jContentPane; } /** * This method initializes jJMenuBar * * @return javax.swing.JMenuBar */ private JMenuBar getJJMenuBar() { if (jJMenuBar == null) { jJMenuBar = new JMenuBar(); jJMenuBar.add(getJMenu()); } return jJMenuBar; } /** * This method initializes jMenu * * @return javax.swing.JMenu */ private JMenu getJMenu() { if (jMenu == null) { jMenu = new JMenu(); jMenu.setText("菜单"); jMenu.add(getJMenuItem()); } return jMenu; } /** * This method initializes jMenuItem * * @return javax.swing.JMenuItem */ private JMenuItem getJMenuItem() { if (jMenuItem == null) { jMenuItem = new JMenuItem(); jMenuItem.setText("弹出窗口"); /* * 这一块是关键性代码!!!!!! * 这一块是关键性代码!!!!!! */ jMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { ///MyOut是你要弹出的窗体的类 MyOut myout=new MyOut(); } }); /* * 这一块是关键性代码!!!!!! */ } return jMenuItem; } }
****针对楼主的补充说明,我已经作了相应的修改了****
关键的代码是如这样子的:
jComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
int nIndex=jComboBox.getSelectedIndex();
////然后针对不同的nIndex值(即不同的被选项)而写入不同的代码。
}
});
我这里帮你编写了一个非常简单的案例,你可以运行看看。
代码如下:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JComboBox;
import java.awt.Rectangle;
import javax.swing.JLabel;
public class JianTing extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JComboBox jComboBox = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
/**
* This is the default constructor
*/
public JianTing() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(314, 204);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.setVisible(true);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(51, 89, 65, 18));
jLabel1.setText("选项内容:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(51, 110, 186, 36));
jLabel.setText("");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJComboBox(), null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
}
return jContentPane;
}
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
/////这里是重点代码!!!!
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
jComboBox.setBounds(new Rectangle(62, 25, 170, 27));
jComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
int nIndex=jComboBox.getSelectedIndex();
if(nIndex==0){
jLabel.setText(("选项A"));
}
else if(nIndex==1){
jLabel.setText(("选项B"));
}
else if(nIndex==2){
jLabel.setText(("选项C"));
}
}
});
String[] myList={"选项A","选项B","选项C"};
jComboBox.addItem(myList[0]);
jComboBox.addItem(myList[1]);
jComboBox.addItem(myList[2]);
}
return jComboBox;
}
public static void main(String args[]){
new JianTing();
}
} // @jve:decl-index=0:visual-constraint="10,10"
第一个问题:使用veiwMenuItem.addActionListener()方法添加监听器。具体使用可以查看JDK API
第二个问题,一时想到两种实现方法
1、你要知道,JPanel不单是一个组件,更是一个组件容器,所以你可以往其中添加一个表格组件,而这个表格组件则将你的查询结果显示出来
2、如果你不希望使用其他组件了的话,倒也可以重载paint()方法,直接将你要显示的信息“绘”出来。例如:
public class DisplayPanel extends JPanel{
public void pain(Graphics g){
super.paint(g);
g.drawString(student.getName());
g.drawString(student.getNumber());
.... ....
}
}
然后将这个Panel添加到你原来的位置即可。