JAVA设置左上角图标 和 背景色都不显示
publicclassFirstTryextendsJFrameimplementsActionListener{privatestaticfinallongserial...
public class FirstTry extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
FirstTry.DrawCompent drawCompent = new DrawCompent();
private Toolkit kit = Toolkit.getDefaultToolkit();
private Image img = kit.getImage("ico2.gif");
public FirstTry(){
setTitle("WO");
setIconImage(img);
add(drawCompent);
setVisible(true);
setSize(300,300);
}
/** Draw the paint **/
private class DrawCompent extends JComponent{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
double leftX = 100;
double leftY = 100;
double rightX = 200;
double rightY = 150;
Rectangle2D rect = new Rectangle2D.Double(leftX, leftY, rightX, rightY);
g2.setPaint(Color.blue);
g2.draw(rect);
DrawCompent drawCompent = new DrawCompent();
drawCompent.setBackground(SystemColor.window);
}
}
public static void main(String args[]){
FirstTry firstTry = new FirstTry();
firstTry.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
} 展开
private static final long serialVersionUID = 1L;
FirstTry.DrawCompent drawCompent = new DrawCompent();
private Toolkit kit = Toolkit.getDefaultToolkit();
private Image img = kit.getImage("ico2.gif");
public FirstTry(){
setTitle("WO");
setIconImage(img);
add(drawCompent);
setVisible(true);
setSize(300,300);
}
/** Draw the paint **/
private class DrawCompent extends JComponent{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
double leftX = 100;
double leftY = 100;
double rightX = 200;
double rightY = 150;
Rectangle2D rect = new Rectangle2D.Double(leftX, leftY, rightX, rightY);
g2.setPaint(Color.blue);
g2.draw(rect);
DrawCompent drawCompent = new DrawCompent();
drawCompent.setBackground(SystemColor.window);
}
}
public static void main(String args[]){
FirstTry firstTry = new FirstTry();
firstTry.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
} 展开
展开全部
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class FirstTry extends JFrame{
private static final long serialVersionUID = 1L;
FirstTry.DrawCompent drawCompent = new DrawCompent();
private Toolkit kit = Toolkit.getDefaultToolkit();
private Image img = kit.getImage("D:\\眼泪知道\\图\\jpg小图标\\png-0001.jpg");//我这里设置得行啊 注意你的路径搞对没?
public FirstTry(){
setTitle("WO");
setIconImage(img);
add(drawCompent);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用这种方法关闭 资源
setVisible(true);
setSize(300,300);
getContentPane().setBackground(Color.red);
}
/** Draw the paint **/
private class DrawCompent extends JComponent{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
int leftX = 100;
int leftY = 100;
int rightX = 200;
int rightY = 150;
// Rectangle2D rect = new Rectangle2D.Double(leftX, leftY, rightX, rightY);
g2.setPaint(Color.blue);
g2.fillRect(leftX,leftY, rightX, rightY);//绘制填充矩形 如果你真的想设置背景色 可以使用 setBackColor()
DrawCompent drawCompent = new DrawCompent();
drawCompent.setBackground(SystemColor.window);
}
}
public static void main(String args[]){
FirstTry firstTry = new FirstTry();
//这里用这种方法关闭虚拟机不提倡
// firstTry.addWindowListener(new WindowAdapter() {
//
// public void windowClosing(WindowEvent e){
// System.exit(0);
//
// }
//
// });
}
}
更多追问追答
追问
我是直接放在和java文件一个包下面的 不是用“xx.gif”吗?
g2.setBacground()是设置绘图的背景色吧,有办法设置整个窗口的背景色嘛?
谢谢了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询