使用 JPanel 和 Graphics 2D 的布局管理器

huangapple go评论76阅读模式
英文:

Using Layout Manager with JPanel and Graphics 2D

问题

我想在一个 JPanel 上绘制线条和其他内容然后将其添加到一个 JFrame 中之后使用 **.pack() 方法**我的问题是我不知道如何在这种特定情况下使用布局管理器通常我通过使用 gridBagLayout 将按钮或其他内容添加到面板中我完全理解这一点但是对于 2D 图形我似乎直接在面板上进行绘制因此我无法正确使用 .pack() 方法有人知道如何正确地对该 jPanel 使用 pack() 方法吗我的代码看起来像是这样的

public class NetworkViewPanel extends JPanel implements KeyListener, ActionListener {
    public NetworkViewPanel(NetworkAI network) {
        this.network = network;
        this.netList = network.getLayerList();
        addKeyListener(this);
        setFocusable(true);
        this.setLayout(new GridLayout(2, 2, 2, 2)); // 这样做有意义吗?

    }

    public void paint(Graphics g) {
        super.paint(g);
        g2 = (Graphics2D) g;

        if (showStandardView) {
            drawRectangles();
            drawLines();
        } else {
            drawRectangles();
            drawLinesSpecial(listIndex, xIndex);
        }
    }

    Greetings :)
}
英文:

I want to draw lines and more on a JPanel, add that to a JFrame and using .pack() afterwards. My problem is that I dont get how to use a Layout Manager in that particular case. Usually I add a button or something to the panel by using a gridBagLayout and I totally understand that. But with graphics 2D I kind of just draw directly to the panel. Therfore I cant use .pack() properly. Does somebody know how to pack() that jPanel the right way? My code looks like that:

public class NetworkViewPanel extends JPanel implements KeyListener, ActionListener {    	
public NetworkViewPanel(NetworkAI network) {
	this.network = network;
	this.netList = network.getLayerList();
	addKeyListener(this);
	setFocusable(true);
	this.setLayout(new GridLayout(2, 2, 2, 2)); // does that even make sense ?

}

public void paint(Graphics g) {
	super.paint(g);
	g2 = (Graphics2D) g;


	if (showStandardView) {
		drawRectangles();
		drawLines();
	} else {
		drawRectangles();
		drawLinesSpecial(listIndex, xIndex);
	}
}

Greetings 使用 JPanel 和 Graphics 2D 的布局管理器

答案1

得分: 2

你可以使用布局管理器对 JPanel 进行布局,并在其上方进行自定义绘制。
这不会阻止你使用 pack()<sup>1</sup>。
以下是使用 GridLayoutJPanel 上绘制线条的示例 mre<sup>2</sup> :

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class NetworkViewPanel extends JPanel{

    private final List&lt;JLabel&gt; labels;

    public NetworkViewPanel() {
        this.setLayout(new GridLayout(2, 2, 2, 2));
        this.setPreferredSize(new Dimension(400,300));//used by pack()
        labels = new ArrayList&lt;&gt;();
        addLabels(new String[]{ "A", "B" , "C" , "D"});
    }

    private void addLabels(String[] text){

        for(String t: text){
            JLabel label = new JLabel(t);
            label.setBorder(BorderFactory.createLineBorder(Color.BLUE));
            label.setHorizontalAlignment(JLabel.CENTER);
            add(label);
            labels.add(label);
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); //draw panel as layed out by layout manager
        drawLines(g);
    }

    private void drawLines(Graphics g) {
        //draw line between centers of first and last components
        int x1 = labels.get(0).getBounds().x + labels.get(0).getBounds().width /2;
        int y1 = labels.get(0).getBounds().y + labels.get(0).getBounds().height /2;
        int x2 = labels.get(labels.size()-1).getBounds().x + labels.get(labels.size()-1).getBounds().width/2;
        int y2 = labels.get(labels.size()-1).getBounds().y + labels.get(labels.size()-1).getBounds().height/2;
        g.setColor(Color.RED);
        g.drawLine(x1, y1, x2, y2);
    }

    public static void main(String[] args) {

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.add(new NetworkViewPanel());
        f.pack();
        f.setVisible(true);
    }
}

<hr>
<sup>1</sup> 参见:.pack() 是什么? <br>
<sup>2</sup> 在提问或回答时请考虑发布 mre

英文:

You can layout a JPanel with a layout manager, and do custom painting on top of it.<br/>
This does not prevent you from using pack()<sup>1</sup>.<br/>
The following mre <sup>2</sup> demonstrates painting a line on a JPanel using a GridLayout: <br/>

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class NetworkViewPanel extends JPanel{
private final List&lt;JLabel&gt; labels;
public NetworkViewPanel() {
this.setLayout(new GridLayout(2, 2, 2, 2));
this.setPreferredSize(new Dimension(400,300));//used by pack()
labels = new ArrayList&lt;&gt;();
addLabels(new String[]{ &quot;A&quot;, &quot;B&quot; , &quot;C&quot; , &quot;D&quot;});
}
private void addLabels(String[] text){
for(String t: text){
JLabel label = new JLabel(t);
label.setBorder(BorderFactory.createLineBorder(Color.BLUE));
label.setHorizontalAlignment(JLabel.CENTER);
add(label);
labels.add(label);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g); //draw panel as layed out by layout manager
drawLines(g);
}
private void drawLines(Graphics g) {
//draw line between centers of first and last components
int x1 = labels.get(0).getBounds().x + labels.get(0).getBounds().width /2;
int y1 = labels.get(0).getBounds().y + labels.get(0).getBounds().height /2;
int x2 = labels.get(labels.size()-1).getBounds().x + labels.get(labels.size()-1).getBounds().width/2;
int y2 = labels.get(labels.size()-1).getBounds().y + labels.get(labels.size()-1).getBounds().height/2;
g.setColor(Color.RED);
g.drawLine(x1, y1, x2, y2);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.add(new NetworkViewPanel());
f.pack();
f.setVisible(true);
}
}

<hr>
<sup>1</sup> See: What does .pack() do? <br>
<sup>2</sup> Consider posting mre when asking or answering

huangapple
  • 本文由 发表于 2020年3月16日 20:11:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/60705784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定