我想让textLb变颜色但ratio按钮不好使,哪位大侠帮我看看import javax.swing.*;
import javax.swing.border.Border;import java.awt.*;
import java.awt.event.*;public class test15_1 {
public static void main(String[] args)
{
CheckBox s = new CheckBox();
s.setTitle("两个按钮");
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setSize(400 ,150 );
s.setLocationRelativeTo(null);
s.setVisible(true);
}
}
class CheckBox extends Show
{
private JRadioButton red = new JRadioButton("red" , true);
private JRadioButton blue = new JRadioButton("blue");
private JRadioButton yellow = new JRadioButton("yellow");
private ButtonGroup group = new ButtonGroup();
private JPanel checkButtonPanel = new JPanel();
CheckBox()
{
setLayout(new GridLayout( 2 ,1));
add(checkButtonPanel);
checkButtonPanel.add(red);
checkButtonPanel.add(blue);
checkButtonPanel.add(yellow);
red.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MyPanel.textLb.setBackground(Color.RED);
repaint();
}
});
blue.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MyPanel.textLb.setBackground(Color.BLUE);
repaint();
}
});
group.add(red);
group.add(blue);
group.add(yellow);
}
}
class Show extends JFrame
{
Show()
{
setLayout(new BorderLayout());
add(new MyPanel() , BorderLayout.CENTER);
} }class MyPanel extends JPanel
{
private JButton left = new JButton("=>");
private JButton right = new JButton("<=");
private JPanel ButtonPanel = new JPanel();
static JLabel textLb = new JLabel();//在一个外部类想引用一个类的局部变量时可以将这个变量定义成final static类型再通过类名
int xTextLb = 130;
int yTextLb = 15 ; MyPanel()
{
setLayout(new BorderLayout());
add(textLb ,BorderLayout.CENTER);
add(ButtonPanel , BorderLayout.SOUTH);
ButtonPanel.add(right);
ButtonPanel.add(left);
System.out.println(xTextLb);
System.out.println(yTextLb);
left.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
xTextLb += 10;
System.out.println(xTextLb);
}
});
right.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
xTextLb -= 10;
System.out.println(xTextLb);
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("WELCOME TO JAVA", xTextLb, yTextLb);
repaint();
}
}
import javax.swing.border.Border;import java.awt.*;
import java.awt.event.*;public class test15_1 {
public static void main(String[] args)
{
CheckBox s = new CheckBox();
s.setTitle("两个按钮");
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setSize(400 ,150 );
s.setLocationRelativeTo(null);
s.setVisible(true);
}
}
class CheckBox extends Show
{
private JRadioButton red = new JRadioButton("red" , true);
private JRadioButton blue = new JRadioButton("blue");
private JRadioButton yellow = new JRadioButton("yellow");
private ButtonGroup group = new ButtonGroup();
private JPanel checkButtonPanel = new JPanel();
CheckBox()
{
setLayout(new GridLayout( 2 ,1));
add(checkButtonPanel);
checkButtonPanel.add(red);
checkButtonPanel.add(blue);
checkButtonPanel.add(yellow);
red.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MyPanel.textLb.setBackground(Color.RED);
repaint();
}
});
blue.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MyPanel.textLb.setBackground(Color.BLUE);
repaint();
}
});
group.add(red);
group.add(blue);
group.add(yellow);
}
}
class Show extends JFrame
{
Show()
{
setLayout(new BorderLayout());
add(new MyPanel() , BorderLayout.CENTER);
} }class MyPanel extends JPanel
{
private JButton left = new JButton("=>");
private JButton right = new JButton("<=");
private JPanel ButtonPanel = new JPanel();
static JLabel textLb = new JLabel();//在一个外部类想引用一个类的局部变量时可以将这个变量定义成final static类型再通过类名
int xTextLb = 130;
int yTextLb = 15 ; MyPanel()
{
setLayout(new BorderLayout());
add(textLb ,BorderLayout.CENTER);
add(ButtonPanel , BorderLayout.SOUTH);
ButtonPanel.add(right);
ButtonPanel.add(left);
System.out.println(xTextLb);
System.out.println(yTextLb);
left.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
xTextLb += 10;
System.out.println(xTextLb);
}
});
right.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
xTextLb -= 10;
System.out.println(xTextLb);
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("WELCOME TO JAVA", xTextLb, yTextLb);
repaint();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货