import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FrameTest{
public static void main(String[] args){
easyFrame frame = new easyFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true); }
}class easyFrame extends JFrame{
public easyFrame(){
Container con = getContentPane();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
setTitle("这是我的第一个窗口");
setResizable(false);
setSize(width / 2,height / 2);
setLocationByPlatform(true);
JPanel ButtonPanel = new JPanel();
add(ButtonPanel);
JButton bt1 = new JButton("ok");
ButtonPanel.add(bt1);
Action bt1Action = new Action();
bt1.addActionListener(bt1Action);
}
class Action implements ActionListener{
public void actionPerformed(ActionEvent e){
setBackground(Color RED);
}
}
}
求问怎么回事,难道我必须写一个类继承JPanel类然后再加按钮、加监听器?
import java.awt.*;
import java.awt.event.*;
public class FrameTest{
public static void main(String[] args){
easyFrame frame = new easyFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true); }
}class easyFrame extends JFrame{
public easyFrame(){
Container con = getContentPane();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
setTitle("这是我的第一个窗口");
setResizable(false);
setSize(width / 2,height / 2);
setLocationByPlatform(true);
JPanel ButtonPanel = new JPanel();
add(ButtonPanel);
JButton bt1 = new JButton("ok");
ButtonPanel.add(bt1);
Action bt1Action = new Action();
bt1.addActionListener(bt1Action);
}
class Action implements ActionListener{
public void actionPerformed(ActionEvent e){
setBackground(Color RED);
}
}
}
求问怎么回事,难道我必须写一个类继承JPanel类然后再加按钮、加监听器?
setBackground(Color.RED);
}
你这样写就是给easyFrame这个类setBackground
而easyFrame已经完全被你的JPanel给挡住了,所以你设颜色也看不出来
要给你的那个JPanel来setBackgroundimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class FrameTest {
public static void main(String[] args) {
easyFrame frame = new easyFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true); }
}class easyFrame extends JFrame {
JPanel ButtonPanel = new JPanel();
public easyFrame() {
Container con = getContentPane();
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
setTitle("这是我的第一个窗口");
setResizable(false);
setSize(width / 2, height / 2);
setLocationByPlatform(true);
add(ButtonPanel);
JButton bt1 = new JButton("ok");
ButtonPanel.add(bt1);
Action bt1Action = new Action();
bt1.addActionListener(bt1Action);
} class Action implements ActionListener {
public void actionPerformed(ActionEvent e) {
ButtonPanel.setBackground(Color.RED);
}
}
}
final JPanel ButtonPanel = new JPanel();
add(ButtonPanel);
JButton bt1 = new JButton("ok");
ButtonPanel.add(bt1);
bt1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
ButtonPanel.setBackground(Color.RED);
}});
我把JPanel ButtonPanel = new JPanel();
这个JPanel面板写进了easyFrame()这个构造函数里面初始化。
代表的意思 就是:我用JPanel面板 遮住或者覆盖 了easyFrame框架吗?然后我把JPanel面板从easyFrame()这个构造函数里面移动到外边。
代表的意思 就是:我没有 遮住 easyFrame框架 状态是:产生了JPanel和easyFrame两层容器。是吗?谢谢。。初学。。
你的JPanel盖住了easyFrame是
add(ButtonPanel);
你给JFrame添加组件的缘故,和你在哪初始化的没关系而把
JPanel ButtonPanel = new JPanel();
从构造函数中提出来是为了让你的这个局部变量成为这个类的全局变量
否则你的
class Action 这个类中,无法获得ButtonPanel的引用,
从而无法对ButtonPanel.setBackground
意思是:
我在easyFrame框架里边 添加了一个我自己实例化(ButtonPanel)的一个面板。等于这两个层成了包容的关系。easyFrame包含ButtonPanel。对吗?。。我又有问题了
那我如果把ButtonPanel单独写成一个类呢。就是这样:
class easyFrame extends JFrame {
ButtonPanel panel = new ButtonPanel();
public easyFrame(){
add(ButtonPanel);
}
}
class ButtonPanel extends JPanel{
..这里边放一个按钮
}
class Action implements ActionListener {
}问题是:JPanel ButtonPanel = new JPanel();
我写在easyFrame的构造函数里 还是外边?我单写成一个类,和不单写成一个类,有区别么
你没必要搞这么多内部类,
像你的Action也完全没必要写成个单独的类