Java 使用GridBagConstraints添加图形组件
我正在寻找一种方法,将使用图形的JC组件与其他JC组件(如JRadioButton)放在一起 因此,我试图在我的JPanel中构建一个GridBagConstraints,以便于放置。但是我不知道如何使用正确的约束将图形组件放置在网格上。如果添加约束,则会显示图形的一小部分 我的代码如下:Java 使用GridBagConstraints添加图形组件,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,我正在寻找一种方法,将使用图形的JC组件与其他JC组件(如JRadioButton)放在一起 因此,我试图在我的JPanel中构建一个GridBagConstraints,以便于放置。但是我不知道如何使用正确的约束将图形组件放置在网格上。如果添加约束,则会显示图形的一小部分 我的代码如下: public class Interface extends JFrame{ private Visualization v; private GridBagConstraints const
public class Interface extends JFrame{
private Visualization v;
private GridBagConstraints constraints=new GridBagConstraints();
private JRadioButton temperature,pluviometry;
public Interface(Visualization v){
this.setTitle("Pluviometry Data Viewer");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.v=v;
//this.getContentPane().add(v);
//this.getContentPane().setLayout(new GridBagLayout());
constraints.gridx=0;
constraints.gridy=0;
constraints.gridheight=(int)v.getPreferredSize().getSize().getHeight();
constraints.gridwidth=(int)v.getPreferredSize().getSize().getWidth();
//constraints.fill=BOTH;
this.add(v);
//this.add(new JRadioButton("Temperature", true),constraints);
this.setVisible(true);
this.pack();
this.setSize(new Dimension(540, 360));
}
}
public class Visualization extends JComponent {
Data data;
private int y_axis=210;
private int x_axis=450;
public Visualization(Data d){
this.data=d;
}
@Override
public void paintComponent(Graphics g){
int width=getWidth();
int height=getHeight();
g.setColor(new Color(255, 78, 23));
g.drawLine(50, 20, 50, 230);
g.drawLine(50, 230, 500, 230);
for(int i=100;i<=500;i+=50){
g.drawLine(i, 230, i, 240);
}
for(int i=180;i>0;i-=40){
g.drawLine(50, i, 40, i);
}
}
@Override
public Dimension getPreferredSize(){
return new Dimension(200,100);
}
}
公共类接口扩展JFrame{
私家侦探v;
私有GridBagConstraints=新GridBagConstraints();
私人日射布顿温度、雨量计;
公共界面(可视化v){
本文件为setTitle(“雨量测量数据查看器”);
此.setDefaultCloseOperation(关闭时退出);
这个,v=v;
//this.getContentPane().add(v);
//this.getContentPane().setLayout(新的GridBagLayout());
约束条件。gridx=0;
约束条件。gridy=0;
constraints.gridheight=(int)v.getPreferredSize().getSize().getHeight();
constraints.gridwidth=(int)v.getPreferredSize().getSize().getWidth();
//约束。填充=两者;
本条增补(v);
//添加(新JRadioButton(“温度”,true),约束);
此.setVisible(true);
这个包();
此.设置尺寸(新尺寸(540360));
}
}
公共类可视化扩展了JComponent{
数据;
私有int y_轴=210;
专用int x_轴=450;
公共可视化(数据d){
这个数据=d;
}
@凌驾
公共组件(图形g){
int width=getWidth();
int height=getHeight();
g、 setColor(新颜色(255,78,23));
g、 抽绳(50、20、50、230);
g、 抽绳(50230500230);
对于(int i=100;i0;i-=40){
g、 抽绳(50,i,40,i);
}
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200100);
}
}
谢谢。有一些小问题
GridBagLayout
可视化时,从不向容器提供GridBagConstraints
Visualization
的preferredSize
与实际渲染的内容不匹配setVisible
lastJFrame
上调用setSize
gridwidth
和gridheight
是在GridBagLayout
上下文中的单元格中测量的,而不是像素import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestGUI extends JFrame {
private Visualization v;
private GridBagConstraints constraints = new GridBagConstraints();
private JRadioButton temperature, pluviometry;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
Visualization v = new Visualization();
TestGUI frame = new TestGUI(v);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public TestGUI(Visualization v) {
this.setTitle("Pluviometry Data Viewer");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.v = v;
setLayout(new GridBagLayout());
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.NORTHWEST;
add(new JRadioButton("Cool stuff"), constraints);
constraints.gridx = 1;
constraints.gridy = 0;
constraints.fill=GridBagConstraints.BOTH;
this.add(v, constraints);
//this.add(new JRadioButton("Temperature", true),constraints);
this.pack();
this.setVisible(true);
}
public static class Visualization extends JComponent {
private int y_axis = 210;
private int x_axis = 450;
public Visualization() {
}
@Override
public void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
g.setColor(new Color(255, 78, 23));
g.drawLine(50, 20, 50, 230);
g.drawLine(50, 230, 500, 230);
for (int i = 100; i <= 500; i += 50) {
g.drawLine(i, 230, i, 240);
}
for (int i = 180; i > 0; i -= 40) {
g.drawLine(50, i, 40, i);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(500 + 50, 230 + 50);
}
}
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
在关闭时导入静态javax.swing.JFrame.EXIT_;
导入javax.swing.JRadioButton;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类TestGUI扩展了JFrame{
私家侦探v;
私有GridBagConstraints=新GridBagConstraints();
私人日射布顿温度、雨量计;
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
可视化v=新的可视化();
TestGUI框架=新的TestGUI(v);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
公共测试GUI(可视化v){
本文件为setTitle(“雨量测量数据查看器”);
此.setDefaultCloseOperation(关闭时退出);
这个,v=v;
setLayout(新的GridBagLayout());
constraints.gridx=0;
constraints.gridy=0;
constraints.anchor=gridbagsconstraints.NORTHWEST;
添加(新的JRadioButton(“酷东西”)、约束条件;
constraints.gridx=1;
constraints.gridy=0;
constraints.fill=gridbagsconstraints.BOTH;
添加(v,约束条件);
//添加(新JRadioButton(“温度”,true),约束);
这个包();
此.setVisible(true);
}
公共静态类可视化扩展了JComponent{
私有int y_轴=210;
专用int x_轴=450;
公众形象化(){
}
@凌驾
公共组件(图形g){
int width=getWidth();
int height=getHeight();
g、 setColor(新颜色(255,78,23));
g、 抽绳(50、20、50、230);
g、 抽绳(50230500230);
对于(int i=100;i 0;i-=40){
g、 抽绳(50,i,40,i);
}
}
@凌驾
公共维度getPreferredSize(){
返回新尺寸(500+50、230+50);
}
}
}
您已经注释掉了对setLayout(新的GridBagLayout())的调用代码>。请理解GridBagConstraints比无意义更糟糕,除非它们用于使用容器向GridBagLayout添加组件。我之所以说“比无意义更糟糕”,是因为使用它们会产生误导,而使用其他布局管理器可能会导致不可预测的布局,甚至引发异常。