问题是由于JFrame没有更新,当我点击"课程浏览","不及格","优秀"按钮时出现的界面看不到!
解决办法是将JFrame作为参数在StudentCourseBrowser初始化时传过去,然后再按钮响应事件中操作;
程序代码有点长,我放在我的百度博客里面 题目是"请帮我实现JFrame的更新."
以下是网址!
http://hi.baidu.com/%CD%F2%B4%BA%C0%F6/blog/item/384eee4fefed1e30afc3ab34.html
请问我的java程序中JFrame为什么没有更新?急急急!请帮忙!
答案:1 悬赏:30 手机版
解决时间 2021-04-15 23:09
- 提问者网友:浩歌待明月
- 2021-04-15 09:35
最佳答案
- 五星知识达人网友:妄饮晩冬酒
- 2021-04-15 10:31
借用了你的一部分代码,点击按钮可以实现数据的更新~~别的部分你自己改改啊~~
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudentCourse implements ActionListener{
String [][]data={
{"row1-col1", "row1-col2"},
{"row2-col1", "row2-col2"}};
String []head={"学号","课程号"};
JButton ButtonFail,ButtonExcellent,ButtonBrowse;
JFrame m;JTable table;JPanel p;
public StudentCourse()
{
m=new JFrame();
setpl(p,data,head);
m.setVisible(true);
m.setSize(500, 300);
m.setTitle("课程信息浏览!");
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setpl(JPanel p,String [][]data,String []head)
{
this.p =p;this.data =data;this.head =head;
p=new JPanel();
table=new JTable(data,head);
table.setLayout(null);//这里用的十分精典.
JScrollPane ScrollBar=new JScrollPane(table);
ScrollBar.setBounds(20, 20, 400, 200);
p.setLayout(null);
p.add(ScrollBar);
ButtonBrowse=new JButton("课程浏览");
ButtonBrowse.setBounds(20, 250, 100, 25);
p.add(ButtonBrowse);
ButtonFail=new JButton("不及格");
ButtonFail.setBounds(20, 290, 80, 25);
p.add(ButtonFail);
ButtonExcellent=new JButton("优秀");
ButtonExcellent.setBounds(20, 330, 80, 25);
p.add(ButtonExcellent);
ButtonFail.addActionListener(this);
ButtonExcellent.addActionListener(this);
ButtonBrowse.addActionListener(this);
m.getContentPane().add(p);
}
public void actionPerformed(ActionEvent e1)
{
if(e1.getSource()==ButtonBrowse)
{
JPanel pp = null;
String [][]data1={
{"row1-col111", "row1-col222"},
{"row2-col111", "row2-col222"}};
String []head1={"学号11","课程号22"};
setpl(pp,data1,head1);
m.setVisible(true);
System.out.println("说明这部分代码已运行!");
}
if(e1.getSource()==ButtonFail)
{
System.out.println("说明这部分代码已运行!");
}
if(e1.getSource()==ButtonExcellent)
{
System.out.println("说明这部分代码已运行!");
}
}
public static void main(String[] args) {
new StudentCourse();
}
}
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudentCourse implements ActionListener{
String [][]data={
{"row1-col1", "row1-col2"},
{"row2-col1", "row2-col2"}};
String []head={"学号","课程号"};
JButton ButtonFail,ButtonExcellent,ButtonBrowse;
JFrame m;JTable table;JPanel p;
public StudentCourse()
{
m=new JFrame();
setpl(p,data,head);
m.setVisible(true);
m.setSize(500, 300);
m.setTitle("课程信息浏览!");
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setpl(JPanel p,String [][]data,String []head)
{
this.p =p;this.data =data;this.head =head;
p=new JPanel();
table=new JTable(data,head);
table.setLayout(null);//这里用的十分精典.
JScrollPane ScrollBar=new JScrollPane(table);
ScrollBar.setBounds(20, 20, 400, 200);
p.setLayout(null);
p.add(ScrollBar);
ButtonBrowse=new JButton("课程浏览");
ButtonBrowse.setBounds(20, 250, 100, 25);
p.add(ButtonBrowse);
ButtonFail=new JButton("不及格");
ButtonFail.setBounds(20, 290, 80, 25);
p.add(ButtonFail);
ButtonExcellent=new JButton("优秀");
ButtonExcellent.setBounds(20, 330, 80, 25);
p.add(ButtonExcellent);
ButtonFail.addActionListener(this);
ButtonExcellent.addActionListener(this);
ButtonBrowse.addActionListener(this);
m.getContentPane().add(p);
}
public void actionPerformed(ActionEvent e1)
{
if(e1.getSource()==ButtonBrowse)
{
JPanel pp = null;
String [][]data1={
{"row1-col111", "row1-col222"},
{"row2-col111", "row2-col222"}};
String []head1={"学号11","课程号22"};
setpl(pp,data1,head1);
m.setVisible(true);
System.out.println("说明这部分代码已运行!");
}
if(e1.getSource()==ButtonFail)
{
System.out.println("说明这部分代码已运行!");
}
if(e1.getSource()==ButtonExcellent)
{
System.out.println("说明这部分代码已运行!");
}
}
public static void main(String[] args) {
new StudentCourse();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯