嘿我试图制作一个天气小程序,但我不理解j面板每一次覆盖其他heres我最终想要它看起来像
在我得到这部分代码后,我将制作一个if else satement,绘制依赖于“Season”的图像
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
public class Test21 extends JApplet
{
JPanel jPanel1, jPanel2;
JButton Enter , Exit;
JTextField location;
JLabel city;
JRadioButton time;
JComboBox Seasons;
Random rand = new Random ();
int P = rand.nextInt (100) + 1; //Random Precipitation
int H = rand.nextInt (50) + 1; //Random Heat
/** Initializes the applet Test02 */
public void init ()
{
jPanel2 = new DrawingPanel ();
getContentPane ().setLayout (null);
//);
getContentPane ().add (jPanel2);
jPanel2.setBounds (20, 20, 320, 240);
jPanel1 = new DrawingPanel();
getContentPane ().setLayout (null);
jPanel1.setBackground (new java.awt.Color (255, 255, 255));
//);
getContentPane ().add (jPanel1);
jPanel1.setBounds (20, 20, 320, 240);
getContentPane() .setLayout (new FlowLayout ());
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?");
getContentPane().add (city);
getContentPane().add (location);
getContentPane().add (Seasons);
getContentPane().add (time);
}
public void paintComponent (Graphics g)
{
super.paint (g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine (10, 70, 70, 10);
}
private class DrawingPanel extends javax.swing.JPanel
{
public void paint (Graphics g)
{
super.paint (g);
Graphics2D g2d = (Graphics2D) g;
drawBars (g);
}
}
public void drawBars (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)
//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)
}
}
这是我班上的一个重要标志请不要告诉我为什么老师不应该教CS或者其中任何一个它不能帮助我在编程时也使用Ready java所以如果你可以尝试保持相同的数据包我我非常缺乏经验并且迷失了,但我需要在星期一完成这项工作,并且已经长期工作了
好的,所以即时尝试一个带边框布局的新文件,并创建下面的类,并尝试调用它们我猜?
public class Final
{
JPanel jPanel1, jPanel2;
JButton Enter, Exit;
JTextField location;
JLabel city;
JRadioButton time;
JComboBox Seasons;
Random rand = new Random ();
int P = rand.nextInt (100) + 1; //Random Precipitation
int H = rand.nextInt (50) + 1; //Random Heat
public void init ()
{
JPanel contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
contentPane.add (locationGui, BorderLayout.NORTH);
contentPane.add (temp, BorderLayout.EAST);
contentPane.add (Percip (g), BorderLayout.SOUTH);
contentPane.add (image, BorderLayout.CENTER);
}
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)
}
}
这是现在我正在使用的代码我得到了位置gui部分,但当我尝试tlabel = new DrawingPanel();它说不能这样做我怎么把这个图形带入?并且我非常确定需要使用actionlistener,所以不得不稍后使用实现添加它
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 JLabel ("Percip");
pLabel.setFont (font);
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
{
public void paint (Graphics g)
{
super.paint (g);
Graphics2D g2d = (Graphics2D) g;
drawPercip (g);
}
}
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)
}
}
答案 0 :(得分:3)
JApplet
有没有paintComponent
方法,因此您实际上并没有覆盖任何内容。作为良好的做法,您应该使用@Override
注释来解决您正在进行有效覆盖的问题。 JApplet
确实拥有paint
方法。paint
中的JPanel
。而是覆盖paintComponent
并致电super.paintComponent
更新
Container
。你可以使它= JPanel
请参阅下面的示例,了解BorderLayout
类Final
的工作原理。
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;
public class Final extends JFrame{
JPanel locationGui = new JPanel();
JPanel temp = new JPanel();
JPanel percip= new JPanel();
JPanel image = new JPanel();
public Final() {
init();
}
public void init() {
Font font = new Font("impact", Font.PLAIN, 20);
locationGui.setBackground(Color.RED);
JLabel guiLabel = new JLabel("Location Gui");
guiLabel.setFont(font);
locationGui.add(guiLabel);
temp.setBackground(Color.BLUE);
temp.setLayout(new GridBagLayout());
JLabel tempLabel = new JLabel("Temp");
tempLabel.setFont(font);
temp.add(tempLabel);
percip.setBackground(Color.GREEN);
JLabel gLabel = new JLabel("Percip");
gLabel.setFont(font);
percip.add(gLabel);
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(){
@Override
public void run() {
new Final();
}
});
}
}
temp = new DrawingPanel()
时,它有效,但有两个问题
BorderLayout
中定义的尺寸。要解决此问题,请覆盖getPreferredSize
DrawingPanel
fillRect
未显示,因为它与背景颜色相同。以下是您的代码的修复程序。
private class DrawingPanel extends javax.swing.JPanel {
@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) {
g.setColor(Color.black);
g.drawRect(40, 170, 100, 20);
g.setColor(Color.blue);
g.fillRect(40 + 1, 170 + 4, P, 14);
}
答案 1 :(得分:1)
您发布的图片可以使用BorderLayout实现。推荐阅读:http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
JPanel 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);