- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,我正在尝试制作一个天气小程序,但我不明白 j 面板每次覆盖另一个面板时我希望它最终看起来像这样
在我得到这部分代码后,我将制作一个 if else 语句,根据“季节”绘制图像
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(); 时它说无法完成,那么我该如何将该图形引入?我很确定我需要使用 Action 监听器,所以我必须稍后将其添加到实现中
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)
}
}
最佳答案
JApplet
没有没有 paintComponent
方法,因此您实际上并没有覆盖任何内容。作为良好实践,您应该使用 @Override
注释来确保您正在进行有效的覆盖。 JApplet
确实有一个paint
方法。JPanel
中的paint
。而是重写 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
中定义的尺寸。要解决此问题,请覆盖 DrawingPanel
的 getPreferredSize
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);
}
关于java - 2个jpanel但不知道如何控制它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21311954/
大家好,我完成了这个基本的 C 程序,它向输入任何给定数字集的用户显示有序集、最小值、最大值、平均值和中值。我遇到的问题是,当我打印数字时,我必须使用诸如“3.2%f”之类的东西来设置标准的精度,我怎
我有这个基于 Python 的服务守护进程,它正在执行大量多路复用 IO(选择)。 从另一个脚本(也是 Python)我想查询这个服务守护进程的状态/信息和/或控制处理(例如暂停它、关闭它、更改一些参
我读到 Fortran 对表达式求值的顺序有严格的规则。对于某些数值算法来说,这一点非常重要。 数值 C 程序如何控制浮点运算的顺序并防止编译器“优化”到不需要的运算顺序,例如将 (a*b)*c 更改
上下文: 整个问题可以概括为我正在尝试复制调用system(或fork)的行为,但在 mpi 环境中。 (事实证明,你不能并行调用system。)这意味着我有一个程序在许多节点上运行,每个节点上有一个
我考虑过控制scanf来接受c中的任何输入。我的概念是等待10秒(或任何其他时间)来接受任何输入。10秒后它将退出并且不再接收任何输入。 int main(){ int a,b,c,d; sca
我正在尝试使用生成器停止 setTimeOut 上的执行流程。我究竟做错了什么?我无法让 console.log 每 1500 毫秒退出一次。我是 node 的新手,如果我在做一件非常愚蠢的事情,请不
我希望我的应用程序的 Activity 堆栈包含同一 Activity 的多个实例,每个实例处理不同的数据。因此,我将让 Activity A 在我的 Activity 堆栈中处理数据 a、b、c 和
我有这个 bash 文件,它向设备询问 OpenSSH 的 IP、密码等。 现在,如果我使用 ssh root@ip,我必须输入密码。这真的很烦人。第二;我不能让我的脚本向它发送命令。 这就是我想要的
我正在尝试测试我有权访问的机器的缓存属性。为此,我正在尝试读取内存并对其计时。我改变工作集大小和步幅访问模式以获得不同的测量值。 代码如下所示: clock1 = get_ticks() for (i
我正在尝试编写一个 makefile 来替换用于构建相当大的应用程序的脚本之一。 当前脚本一次编译一个文件,使用 make 的主要原因是并行化构建过程。使用 make -j 16 我目前在办公室服务器
我正在制作一个小的测试程序,它演示了一个粗糙的控制台界面。 该程序是一个低于标准的典型获取行、响应程序,它甚至不识别“退出”,并希望您通过按 control-c 强制退出。在 Mingw32 上完成。
好的,我有一个 VOIP 电话。我知道电话的 IP 地址和端口,并且可以完全访问电话,我正在使用它通过 SIP 中继调用 SIP 电话。 我基本上想随时查看手机上发生的事情,但我不知道从哪里开始。 如
是否可以指定 CWinApp::WriteProfileString() 使用的应用程序名称? 如果我使用 CWinApp::SetRegistryKey 将我的公司名称设置为“MyCompany”,
我正在尝试用 Python 控制 Tor。我在 stackoverflow 上阅读了其他几个关于这个主题的问题,但没有一个能回答这个问题。 我正在寻找一种方法,以便在命令运行时为您提供“新身份”、新
最近在做一个项目,涉及到iPhone设备和手表传输数据、控制彼此界面跳转,在网上找了很多资料,发现国内的网站这方面介绍的不多,而国外的网站写的也不是很全,所以在这写这篇文章,给大家参考一下,望大神指
我想增加图中值的范围。在示例中,值的范围从 50 到 200。但是,我需要按如下方式分配值:50 75 100 125 150 175 200 并且最好使用 scale_fill_gradientn
我有一个IconButton,当按下时波纹效果是圆形的并且比按钮的面积大,我怎样才能减少点击按钮时波纹效果的大小? IconButton( constraints
我正在使用代码契约(Contract)为我的项目生成附属程序集。基本上它为项目的 MyAssembly.dll 创建一个 MyAssembly.Contracts.dll。这应该放在你的程序集旁边,但
我想使用分面绘制图形,其中面板之间的边缘不同。面板按字母顺序自动排序(按照 ggplot 中的惯例)。一个简单的例子: library(igraph) library(ggraph) g <- mak
我想为我的 Android 应用程序创建一个小部件,以显示有关位置的一些实时详细信息,例如天气。但我想在任何时候允许最多 3 个小部件实例,每个实例都有不同的位置。我不确定该怎么做,也找不到任何信息。
我是一名优秀的程序员,十分优秀!