新手有个问题
netbeans吧
全部回复
仅看楼主
level 1
remmeltime 楼主
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GraExp4 extends
JFrame{Container J;
JButton btn=new JButton("选择颜色");
Color color=new Color(200,200,200);
public GraExp4()
{
J=getContentPane();
J.setLayout(new FlowLayout());
J.add(btn);
btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e)
{
color=JColorChooser.showDialog(null,"请选择你喜欢的颜色",color);
if (color==null) color=Color.lightGray;
J.setBackground(color);
J.repaint();
}
}
);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(new
Dimension(400,300));
show();
BB();
{
add(J);
setSize(450, 550);//设置窗口大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置一个默认的关闭操作,点击按钮时,退出程序。
setLocationRelativeTo(null);
setVisible(true);
this.validate();
JButton btn=new JButton("选择颜色");
Color color=new Color(200,200,200);
}
}
public static void main(String[] args)
{
GraExp4 ge=new GraExp4();
new BB();
}
private void BB() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private static class BB {
public BB() {
}
}
}
class MyJPanel extends JPanel implements ComponentListener{
MyJPanel(){
this.setBackground(Color.BLACK);
}
int roses=3;
@Override
public void paint(Graphics g){
super.paint(g);
int x0, y0;
x0 = this.getWidth() / 2;
y0 = this.getHeight() / 2;
g.setColor(Color.RED);
g.drawLine(x0, 0, x0, y0 * 2);
g.drawLine(0, y0, x0 * 2, y0);
List<Integer> listx=new ArrayList<Integer>();
List<Integer> listy=new ArrayList<Integer>();
for (int i = 0; i < 1024; i++) {
double angle = i * Math.PI / 512;
double radius = 200*Math.sin(roses * angle);
int x = (int) Math.round(radius * Math.cos(angle));
int y = (int) Math.round(radius * Math.sin(angle));
listx.add(x0+x);
listy.add(y0+y);
}
for (int i = 0; i < listx.size()-1; i++) {
g.drawLine(listx.get(i),listy.get(i), listx.get(i+1), listy.get(i+1));//画点
}
}
@Override
public void componentHidden(ComponentEvent arg0) {}
@Override
public void componentMoved(ComponentEvent arg0) {}
@Override
public void componentResized(ComponentEvent arg0) {
repaint();
}
@Override
public void componentShown(ComponentEvent arg0) {}
}
怎么让选择颜色的按钮和图像出现在一个面板上,谢谢大家了。
2016年06月11日 04点06分 1
1