菜鸟求助!!Java 的一个主界面有4个buttons,但JButton 并没有出现, 而且我CARDLAYOUT 是胡用的谢谢! 110
importjava.awt.FlowLayout;importjava.awt.CardLayout;importjavax.swing.*;importjava.aw...
import java.awt.FlowLayout;
import java.awt.CardLayout;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Wordspet {
JPanel primary,memorizingwords,pet,library,settings;
JButton M,p,l,s;
private CardLayout a;
private JFrame frame;
// 建立四个面和按钮
public Wordspet(){
frame=new JFrame();
a=new CardLayout();
primary=new JPanel();
memorizingwords=new JPanel();
pet=new JPanel();
library=new JPanel();
settings=new JPanel();
M=new JButton("Memorizingwords");
p=new JButton("Pet");
l=new JButton("Library");
s=new JButton("Settings");
primary.add(M);
primary.add(p);
primary.add(l);
primary.add(s);
M.setBounds(100, 100, 20, 20);
frame.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
primary.setLayout(a);
frame.add(memorizingwords);
memorizingwords.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
pet.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
library.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
settings.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
M.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==M){
a.previous(primary);//返回主菜单
}
}
});
p.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==p){
a.previous(primary);//返回主菜单
}
}
});
l.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==l){
a.previous(primary);//返回主菜单
}
}
});
s.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==s){
a.previous(primary);//返回主菜单
}
}
});
M.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==M){
a.previous(primary);//返回主菜单
}
}
});
frame.setVisible(true);
frame.setSize(1000,800);
frame.setLocation(500,200);
}
public static void main(String[] args){
new Wordspet();
}
} 展开
import java.awt.CardLayout;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Wordspet {
JPanel primary,memorizingwords,pet,library,settings;
JButton M,p,l,s;
private CardLayout a;
private JFrame frame;
// 建立四个面和按钮
public Wordspet(){
frame=new JFrame();
a=new CardLayout();
primary=new JPanel();
memorizingwords=new JPanel();
pet=new JPanel();
library=new JPanel();
settings=new JPanel();
M=new JButton("Memorizingwords");
p=new JButton("Pet");
l=new JButton("Library");
s=new JButton("Settings");
primary.add(M);
primary.add(p);
primary.add(l);
primary.add(s);
M.setBounds(100, 100, 20, 20);
frame.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
primary.setLayout(a);
frame.add(memorizingwords);
memorizingwords.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
pet.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
library.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
settings.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
M.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==M){
a.previous(primary);//返回主菜单
}
}
});
p.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==p){
a.previous(primary);//返回主菜单
}
}
});
l.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==l){
a.previous(primary);//返回主菜单
}
}
});
s.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==s){
a.previous(primary);//返回主菜单
}
}
});
M.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==M){
a.previous(primary);//返回主菜单
}
}
});
frame.setVisible(true);
frame.setSize(1000,800);
frame.setLocation(500,200);
}
public static void main(String[] args){
new Wordspet();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询