簡體   English   中英

在JFrame中添加來自同一類的兩個組件

[英]Adding two components from the same class in a JFrame

因此,對於某些作業,我必須創建一個代表余額的條形圖。 為了熟悉圖形/組件,我只想在屏幕上放置兩個框。 似乎第一個框被“繪制”,然后第二個框被覆蓋? 這是兩個類。

主類-BalanceChart.java

package balancechart;

import javax.swing.*;

public class BalanceChart {

Double[] Props = new Double[6];

public static void main(String[] args) {
   JFrame f = new JFrame("Balance Chart");
   f.setSize(500, 500);
   f.setDefaultCloseOperation(
   JFrame.EXIT_ON_CLOSE);
   ChartComponent ccOne = new ChartComponent(50, 50, 100, 200);
   ChartComponent ccTwo = new ChartComponent(10, 10, 10, 10);
   f.add(ccOne);
   f.add(ccTwo);
   f.setVisible(true);
}

private void getProps(){
    //ignore
}

}

組件類-ChartComponent.java

package balancechart;

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

public class ChartComponent 
extends JComponent {

private int x, y, w, h;

public ChartComponent(int x, int y, int w, int h){
  this.x = x;
  this.y = y;
  this.w = w;
  this.h = h;
}

public void paintComponent(Graphics g){
  Graphics2D g2 = (Graphics2D) g.create();
  g2.setColor(Color.RED);
  g2.fillRect(w, h, x, y);
}

}

我想這與我對JFrame的2種添加過程有關,但我不確定該如何做。

提前致謝

您需要跑步而不是步行到最近的布局管理器教程。 在那里,您會發現JFrame的contentPane默認情況下使用BorderLayout,並且當您將組件添加到使用BorderLayout的容器中而沒有其他常量時,默認情況下會將其添加到BorderLayout.CENTER位置,包括之前添加的所有內容。

一種解決方案是創建另一個使用選擇的布局的JPanel,甚至將其保留為JPanel的默認FlowLayout,然后將組件添加到其中,然后將該JPanel添加到JFrame。

ChartComponent ccOne = new ChartComponent(50, 50, 100, 200);
ChartComponent ccTwo = new ChartComponent(10, 10, 10, 10);
JPanel container = new JPanel(); // JPanel uses FlowLayout by default
container.add(ccOne);
container.add(ccTwo);
f.add(container, BorderLayout.CENTER); // just to be clear
f.pack();
f.setVisible(true);

請查看此教程鏈接: 在容器中布置組件


編輯
正如MadProgrammer指出的那樣,您在這里還有另一個問題:

public void paintComponent(Graphics g){
  Graphics2D g2 = (Graphics2D) g.create();
  g2.setColor(Color.RED);
  g2.fillRect(w, h, x, y);
}

因為您不會調用super方法,因此在需要時不會清除舊圖像,如果您嘗試通過在Swing中更改w,h,x或y來做一些動畫說明,這將非常重要計時器。 要解決此問題,請務必調用super的方法。 另外,paintComponent應該被protected而不是public ,並且應該使用@Override批注以確保正確覆蓋了該方法。 並且,如果您絕對需要創建一個新的Graphics上下文,則應在完成后處置它,以免耗盡資源。 處置由JVM(如一個傳遞到你的給你一個圖形上下文paintComponent(...)方法的參數,因為這會產生有害的副作用:

@Override
protected void paintComponent(Graphics g){
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g.create();
  g2.setColor(Color.RED);
  g2.fillRect(w, h, x, y);
  g2.dispose(); // only do this if you create your own Graphics context
}

編輯2
另外,您應該養成不要設置任何大小的習慣,而應讓GUI的組件和布局管理器在需要時設置自己的大小,因為這將導致更加愉悅和靈活的GUI程序。


編輯3
您發表評論:

我修復了代碼,但是似乎沒有2個紅色正方形出現在任何地方,並且窗口以很小的尺寸啟動。 我仍然有很多學習要做,我看不出為什么添加的代碼可以做到這一點。

您的問題是您的JComponent(ChartComponent的默認preferredSize)將為0,0或1,1; 我忘記了,但是沒關系,因為在任何一種情況下,組件的大小都不足以被看到。 要解決此問題,請為您的類提供一個getPreferredSize()方法重寫,以幫助設置其大小。 例如,:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;    
import javax.swing.*;

public class BalanceChart {

   Double[] Props = new Double[6];

   public static void main(String[] args) {
      JFrame f = new JFrame("Balance Chart");
      //  f.setSize(500, 500);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ChartComponent ccOne = new ChartComponent(50, 50, 100, 200);
      ChartComponent ccTwo = new ChartComponent(10, 10, 10, 10);
      JPanel container = new JPanel();
      container.add(ccOne);
      container.add(ccTwo);
      f.add(container);
      f.pack();
      f.setLocationByPlatform(true);
      f.setVisible(true);
   }

   private void getProps() {
      // ignore
   }

}

class ChartComponent extends JComponent {

   private static final int PREF_W = 400;
   private static final int PREF_H = PREF_W;
   private int x, y, w, h;

   public ChartComponent(int x, int y, int w, int h) {
      this.x = x;
      this.y = y;
      this.w = w;
      this.h = h;
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setColor(Color.RED);
      g2.fillRect(w, h, x, y);
      g2.dispose();
   }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM