Java图形AWT

huangapple go评论62阅读模式
英文:

Java Graphics AWT

问题

以下是翻译好的代码部分:

import java.awt.*;
import java.awt.Graphics;
import javax.swing.*;

public class gfix extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.setColor(Color.red);
       g.fillRect(80, 100, 150, 75);
    }
    
    public static void main(String[] args){
       gfix gg = new gfix();
       JFrame frame = new JFrame("RISK");
       frame.setSize(800, 600);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       JPanel panel = new JPanel();
       panel.setLayout(null);
       frame.add(panel);
       JButton button = new JButton("test");
       button.setBounds(100, 100, 150, 150);
       panel.add(button);
       frame.setVisible(true);
    }
}
英文:

When I run the below code, I am unable to see the background color as red. It's showing default one. Is there anything that I have to add to these lines?

import java.awt.*;
import java.awt.Graphics;
import javax.swing.*;
public class gfix extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.setColor(Color.red);
       g.fillRect(80, 100, 150, 75);
    }
    public static void main(String[] args){
       gfix gg=new gfix();
       JFrame frame = new JFrame("RISK");
       frame.setSize(800, 600);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       JPanel panel = new JPanel();
       panel.setLayout(null);
       frame.add(panel);
       JButton button = new JButton("test");
       button.setBounds(100, 100, 150, 150);
       panel.add(button);
       frame.setVisible(true);
    }
}

答案1

得分: 1

你正在gfix类中重写paintGraphics()方法,所以将gfix类对象添加到你的窗口中,而不是添加Java提供的JPanel类对象。

gfix gg = new gfix();
JFrame frame = new JFrame("RISK");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// JPanel panel = new JPanel();      不需要这一行
// panel.setLayout(null);
frame.add(gg);
JButton button = new JButton("test");
button.setBounds(100, 100, 150, 150);
gg.add(button);
frame.setVisible(true);

对于g.fillRect(80, 100, 150, 75);,请给定适当的面板边界以填充整个面板背景。或者在paintGraphics中使用int width = getWidth(); int height = getHeight();来获取实际的高度和宽度。

英文:

Your are overriding painGraphics() in gfix class so add gfix class object into your frame not Java provided JPanel class object.

       gfix gg=new gfix();
       JFrame frame = new JFrame("RISK");
       frame.setSize(800, 600);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       //JPanel panel = new JPanel();      Not needed
       //panel.setLayout(null);
       frame.add(gg);
       JButton button = new JButton("test");
       button.setBounds(100, 100, 150, 150);
       gg.add(button);
       frame.setVisible(true);

for g.fillRect(80, 100, 150, 75); give proper panel bounds to fill complete panel background. OR use int width = getWidth();
int height = getHeight();
in paintGraphics to get actual height and width.

答案2

得分: 0

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class gfix {

    public static void main(String[] args) {
        JFrame frame = new JFrame("RISK");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setBackground(Color.RED);
        frame.add(panel);
        JButton button = new JButton("test");
        // adjust numbers as needed
        button.setMargin(new Insets(20,40,20,40));
        panel.add(button);
        // adjust numbers as needed
        panel.setBorder(new EmptyBorder(10,40,50,200));
        frame.pack();
        frame.setVisible(true);
    }
}

其他提示:

  1. Java GUI 需要在不同的操作系统、屏幕尺寸、屏幕分辨率等条件下运行,使用不同的 PLAFs(平台相关的观感和感觉)。因此,它们不适合像素完美的布局。而应该使用布局管理器,或者结合布局填充和边框来创建空白间隙。
  2. 请提供 ASCII 艺术或简单绘图,以显示 GUI 的预期布局,包括最小尺寸,如果可调整大小,还应包括更大的宽度和高度,以显示额外的空间应如何使用。
  3. 请学习常见的 Java 命名约定(命名规范),例如EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute,除非它是一个UPPER_CASE_CONSTANT,并保持一致使用。
英文:

Java图形AWT

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class gfix {

    public static void main(String[] args) {
        JFrame frame = new JFrame("RISK");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setBackground(Color.RED);
        frame.add(panel);
        JButton button = new JButton("test");
        // adjust numbers as needed
        button.setMargin(new Insets(20,40,20,40));
        panel.add(button);
        // adjust numbers as needed
        panel.setBorder(new EmptyBorder(10,40,50,200));
        frame.pack();
        frame.setVisible(true);
    }
}

Other tips:

  1. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
  2. Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used.
  3. Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.

huangapple
  • 本文由 发表于 2020年5月3日 23:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61577436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定