gpt4 book ai didi

java - 在 jpanel 中绘制图形 (g)

转载 作者:行者123 更新时间:2023-12-02 06:17:25 30 4
gpt4 key购买 nike

我试图在下面的java小程序中绘制一些具有随机整数的条形图,但是覆盖有一个错误,我是java新手,我试图在代码中标记这两个区域,绘制方法位于最底部,另一个是第二个区域面板面积我的目标是什么

/image/oRdi4.jpg

尺寸 x,y 不是坐标

-总大小 = 200,250

-位置 gui =200,50

-图像 = 140,150

-沉淀= 100,20

-温度 = 20,100

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;

public class Final2 extends JFrame
{
//Panels
JPanel locationGui = new JPanel ();
JPanel temp = new JPanel ();
JPanel percip = new JPanel ();
JPanel image = new JPanel ();
//Location gui components
JButton Enter, Exit;
JTextField location;
JLabel city;
JRadioButton time;
JComboBox Seasons;
//bar # genertor
Random rand = new Random ();
int P = rand.nextInt (100) + 1; //Random Precipitation
int H = rand.nextInt (50) + 1; //Random Heat
public Final2 ()
{
init ();

}


public void init ()
{
Font font = new Font ("impact", Font.PLAIN, 20);
//________________________________________________new panel____________________
locationGui.setBackground (Color.RED);
JLabel guiLabel = new JLabel ("");
guiLabel.setFont (font);

Enter = new JButton ("Enter");
Exit = new JButton ("exit");



city = new JLabel ("What city?");
location = new JTextField (20); //location entry field


Seasons = new JComboBox ();
Seasons.addItem ("Summer");
Seasons.addItem ("Fall");
Seasons.addItem ("Winter");
Seasons.addItem ("Spring");


time = new JRadioButton ("check if night?");


locationGui.add (city);
locationGui.add (location);
locationGui.add (Seasons);
locationGui.add (time);



locationGui.add (guiLabel);


//________________________________________________new panel____________________
temp.setBackground (Color.BLUE);
temp.setLayout (new GridBagLayout ());
JLabel tempLabel = new JLabel ("Temp");
tempLabel.setFont (font);
temp.add (tempLabel);

//________________________________________________new panel____________________
percip.setBackground (Color.GREEN);
JLabel pLabel = new DrawingPanel (); //where it should be

percip.add (pLabel);

//________________________________________________new panel____________________
image.setBackground (Color.ORANGE);
image.setLayout (new GridBagLayout ());
JLabel imageLabel = new JLabel ("Image");
imageLabel.setFont (font);
image.add (imageLabel);

Container contentPane = getContentPane ();

contentPane.setLayout (new BorderLayout ());
contentPane.add (locationGui, BorderLayout.NORTH);
contentPane.add (temp, BorderLayout.EAST);
contentPane.add (percip, BorderLayout.SOUTH);
contentPane.add (image, BorderLayout.CENTER);

setContentPane (contentPane);
setDefaultCloseOperation (EXIT_ON_CLOSE);
setSize (400, 400);
setLocationRelativeTo (null);
setVisible (true);
}


public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable ()

{


public void run ()
{
new Final2 ();
}
}


);
}


private class DrawingPanel extends javax.swing.JPanel {//area i have issues with atm
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawPercip(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}



}


public void drawPercip (Graphics g)
{
//Precipitation Bar
g.setColor (Color.black);
g.drawRect (40, 170, 100, 20); //outline of bar
g.setColor (Color.blue);
g.fillRect (40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar)
}


public void drawTemp (Graphics g)
{
//Temparature Bar
g.setColor (Color.red);
g.fillRect (170 + 4, 50, 14, 100); //Covers in
g.setColor (Color.black);
g.drawRect (170, 50, 20, 100); //outline of bar
g.setColor (Color.white);
g.fillRect (170 + 4, 50 + 1, 16, 100 - H); //indicator bar (+4 puts space beetween outline bar)
}




}

我当前的错误 (4) 发生在 @override 的这段代码中,提供的唯一消息是“非法 token ”和“忽略意外符号”,不知道为什么它不提供更多信息

 private class DrawingPanel extends javax.swing.JPanel {//area i have issues with atm
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawPercip(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}

最佳答案

首先我没有看到小程序,

第二,DrawingPanel 不是 JLabel

JLabel pLabel = new DrawingPanel(); //where it should be

根据您想要的功能,将 pLable 类型更改为 JComponentJPanelDrawingPanel访问每个类。

第三,你不应该依赖魔数(Magic Number)

public void drawPercip(Graphics g) {
//Precipitation Bar
g.setColor(Color.black);
g.drawRect(40, 170, 100, 20); //outline of bar
g.setColor(Color.blue);
g.fillRect(40 + 1, 170 + 4, P, 14); //indicator bar (+4 puts space beetween outline bar)
}

public void drawTemp(Graphics g) {
//Temparature Bar
g.setColor(Color.red);
g.fillRect(170 + 4, 50, 14, 100); //Covers in
g.setColor(Color.black);
g.drawRect(170, 50, 20, 100); //outline of bar
g.setColor(Color.white);
g.fillRect(170 + 4, 50 + 1, 16, 100 - H); //indicator bar (+4 puts space beetween outline bar)
}

无法保证面板的高度和宽度就是您想象的那样。使用 getWidthgetHeight 方法

第四,如果您打算拥有多个 DrawingPanel,您应该制作

int P = rand.nextInt(100) + 1; //Random Precipitation
int H = rand.nextInt(50) + 1; //Random Heat

DrawingPanel的一部分。

您还应该花时间阅读 Code Conventions for the Java Programming Language

关于java - 在 jpanel 中绘制图形 (g),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21323260/

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