java的repaint()没法重绘,怎么回事?
packagechapter17;importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publiccl...
package chapter17;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class showRadioButton extends JFrame{
/**
* @param args
*/
showRadioButton(){
creatButtonPanel bp =new creatButtonPanel();
messagePanel mp = new messagePanel();
setLayout(new BorderLayout());
add(bp,BorderLayout.SOUTH);
add(mp,BorderLayout.CENTER);
messagePanel messagepanel = new messagePanel();
setSize(800,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
showRadioButton srb = new showRadioButton();
}
//生成String,并操作String
class messagePanel extends JPanel{
private String s = "Welcome to Java";
private boolean bl = true;
private int x ;
private int y ;
// 第一次绘制图形
protected void paintComponent(Graphics g){
super.paintComponent(g);
// 设置string的位置
if(bl == true){
FontMetrics fm = g.getFontMetrics();
int sw = fm.stringWidth(s)/2;
int sh = fm.getAscent()/2;
x = getWidth()/2 - sw;
y = getHeight()/2 - sh;
//开始绘制sting并且设置布尔值
g.drawString(s,x,y);
bl = false;
}else{
g.drawString(s,x,y);
}
}
//重绘方法
protected void left(){
x = x - 50;
repaint();
}
public void right() {
x = x + 50;
repaint();
}
}
//绘制button按钮并出册事件
class creatButtonPanel extends JPanel{
public creatButtonPanel(){
JButton jbtl = new JButton("left");
JButton jbtr = new JButton("rigtht");
jbtl.setMnemonic('L');
jbtr.setMnemonic('R');
//showRadioButton srb = new showRadioButton();
add(jbtl);
add(jbtr);
jbtl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new messagePanel().left(); // 重要 外部类调用内部类
}
});
jbtl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new messagePanel().right();
}
});
//return buttonPanel;
}
}
}
在线等呀 展开
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class showRadioButton extends JFrame{
/**
* @param args
*/
showRadioButton(){
creatButtonPanel bp =new creatButtonPanel();
messagePanel mp = new messagePanel();
setLayout(new BorderLayout());
add(bp,BorderLayout.SOUTH);
add(mp,BorderLayout.CENTER);
messagePanel messagepanel = new messagePanel();
setSize(800,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
showRadioButton srb = new showRadioButton();
}
//生成String,并操作String
class messagePanel extends JPanel{
private String s = "Welcome to Java";
private boolean bl = true;
private int x ;
private int y ;
// 第一次绘制图形
protected void paintComponent(Graphics g){
super.paintComponent(g);
// 设置string的位置
if(bl == true){
FontMetrics fm = g.getFontMetrics();
int sw = fm.stringWidth(s)/2;
int sh = fm.getAscent()/2;
x = getWidth()/2 - sw;
y = getHeight()/2 - sh;
//开始绘制sting并且设置布尔值
g.drawString(s,x,y);
bl = false;
}else{
g.drawString(s,x,y);
}
}
//重绘方法
protected void left(){
x = x - 50;
repaint();
}
public void right() {
x = x + 50;
repaint();
}
}
//绘制button按钮并出册事件
class creatButtonPanel extends JPanel{
public creatButtonPanel(){
JButton jbtl = new JButton("left");
JButton jbtr = new JButton("rigtht");
jbtl.setMnemonic('L');
jbtr.setMnemonic('R');
//showRadioButton srb = new showRadioButton();
add(jbtl);
add(jbtr);
jbtl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new messagePanel().left(); // 重要 外部类调用内部类
}
});
jbtl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new messagePanel().right();
}
});
//return buttonPanel;
}
}
}
在线等呀 展开
1个回答
展开全部
package chapter17;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class showRadioButton extends JFrame{
/**
* @param args
*/
messagePanel mp;
showRadioButton(){
creatButtonPanel bp =new creatButtonPanel();
mp = new messagePanel();
setLayout(new BorderLayout());
add(bp,BorderLayout.SOUTH);
add(mp,BorderLayout.CENTER);
messagePanel messagepanel = new messagePanel();
setSize(800,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
showRadioButton srb = new showRadioButton();
}
//生成String,并操作String
class messagePanel extends JPanel{
private String s = "Welcome to Java";
private boolean bl = true;
private int x ;
private int y ;
// 第一次绘制图形
protected void paintComponent(Graphics g){
super.paintComponent(g);
// 设置string的位置
if(bl == true){
FontMetrics fm = g.getFontMetrics();
int sw = fm.stringWidth(s)/2;
int sh = fm.getAscent()/2;
x = getWidth()/2 - sw;
y = getHeight()/2 - sh;
//开始绘制sting并且设置布尔值
g.drawString(s,x,y);
bl = false;
}else{
g.drawString(s,x,y);
}
}
//重绘方法
protected void left(){
x = x - 50;
repaint();
}
public void right() {
x = x + 50;
repaint();
}
}
//绘制button按钮并出册事件
class creatButtonPanel extends JPanel{
public creatButtonPanel(){
JButton jbtl = new JButton("left");
JButton jbtr = new JButton("rigtht");
jbtl.setMnemonic('L');
jbtr.setMnemonic('R');
//showRadioButton srb = new showRadioButton();
add(jbtl);
add(jbtr);
jbtl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
mp.left(); // 重要 外部类调用内部类
}
});
jbtr.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
mp.right();
}
});
//return buttonPanel;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询