gpt4 book ai didi

java - 状态栏,如何使我的线条、椭圆和矩形可见?

转载 作者:行者123 更新时间:2023-12-02 07:35:34 26 4
gpt4 key购买 nike

我已经设置了线条、矩形和椭圆形,但我有一个问题。

如何使我的线条、椭圆形和矩形在 main 方法中可见?由于某种原因,当我编译和运行文件时它们没有被输出......谢谢

 import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Dimension;
import java.awt.Graphics;


import javax.swing.JPanel;

public class LinesRectsOvalsJPanel extends JPanel {


public LinesRectsOvalsJPanel() {


setLayout(new BorderLayout());
setPreferredSize(new Dimension(10, 23));

JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setOpaque(true);

add(rightPanel, BorderLayout.EAST);
}

public void paintComponent( Graphics g ) {

super.paintComponent( g );

this.setBackground( Color.WHITE );
// x y width height
g.setColor(Color.BLACK);
g.drawLine(5,10,5,30);
g.setColor(Color.BLUE);
g.drawLine(18,70,127,24);
g.setColor(Color.RED);
g.drawLine(25,45,100,38);

g.setColor(Color.YELLOW);
g.drawOval(23,25,23,55);
g.setColor(Color.BLACK);
g.drawOval(15,14,40,78);
g.setColor(Color.CYAN);
g.drawOval(180,102,5,90);
g.setColor(Color.RED);
g.drawOval(21,20,89,11);
g.setColor(Color.BLUE);
g.drawOval(35,87,39,27);
g.setColor(Color.YELLOW);
g.fillRect(87,5,5,60);
g.setColor(Color.GREEN);
g.fillRect(105,15,15,85);
g.setColor(Color.CYAN);
g.fillRect(14,45,76,86);
g.setColor(Color.RED);
g.fillRect(70,79,65,86);
g.setColor(Color.BLUE);
g.fillRect(90,108,5,8);
}
}

代码2

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("");
frame.setLayout(new BorderLayout());
frame.setSize(300, 300);

JPanel statusPanel = new JPanel();
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
frame.add(statusPanel, BorderLayout.SOUTH);
statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 16));
statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
JLabel statusLabel = new JLabel("status");
statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(statusLabel);

frame.setVisible(true);
}
}

最佳答案

您尚未添加 LinesRectsOvalsJPanel 组件:

frame.add(new LinesRectsOvalsJPanel());

关于java - 状态栏,如何使我的线条、椭圆和矩形可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12289737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com