如何能让 upJLabel 和 downJLabel 占用的 网格大一点
布局效果 应该是 upJComboBox upJButton upJLabeldownJComboBox downJButton downJLabel 2行大列,如果用 fowlayout布局的话也是平分 网格,因为 我想 upJLabel和downJLabel
占用大些
public class LogoSetPanel extends JPanel
{
//上面的下拉框,可以选择logo图片在导出的excel中为左,或右或不选
private JComboBox upJComboBox = null; //下面的下拉框,可以选择logo图片在导出的excel中为左,或右或不选
private JComboBox downJComboBox = null; //设置logo的upButton和downButton
private JButton upJButton = null; private JButton downJButton = null; private JLabel upJLabel = null; private JLabel downJLabel = null; String[] items = { "不设置", "左边", "右边" }; public LogoSetPanel()
{
initUI();
initLister();
} /**
* 初始化界面
*/
private void initUI()
{ upJComboBox = new JComboBox(items);
downJComboBox = new JComboBox(items);
upJButton = new JButton("设置图片");
downJButton = new JButton("设置图片");
upJLabel = new JLabel(new ImageIcon("E:/1.PNG"));
downJLabel = new JLabel(new ImageIcon("E:/1.PNG"));
// 界面布局
this.setLayout(new java.awt.GridBagLayout());
this.add(upJComboBox, new GridBagConstraints(0, 0, 1, 1, 5, 5, GridBagConstraints.WEST,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
this.add(upJButton, new GridBagConstraints(0, 1, 1, 1, 5, 5, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 6, 0, 6), 0, 0));
this.add(upJLabel, new GridBagConstraints(0, 2, 1, 1, 10, 10, GridBagConstraints.EAST,
GridBagConstraints.BOTH, new Insets(0, 6, 0, 6), 0, 0));
this.add(downJComboBox, new GridBagConstraints(1, 0, 1, 1, 5, 5, GridBagConstraints.WEST,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
this.add(downJButton, new GridBagConstraints(1, 1, 1, 1, 5, 5, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 6, 0, 6), 0, 0));
this.add(downJLabel, new GridBagConstraints(1, 2, 1, 1, 10, 10, GridBagConstraints.EAST,
GridBagConstraints.BOTH, new Insets(0, 6, 0, 6), 0, 0)); }
}
布局效果 应该是 upJComboBox upJButton upJLabeldownJComboBox downJButton downJLabel 2行大列,如果用 fowlayout布局的话也是平分 网格,因为 我想 upJLabel和downJLabel
占用大些
public class LogoSetPanel extends JPanel
{
//上面的下拉框,可以选择logo图片在导出的excel中为左,或右或不选
private JComboBox upJComboBox = null; //下面的下拉框,可以选择logo图片在导出的excel中为左,或右或不选
private JComboBox downJComboBox = null; //设置logo的upButton和downButton
private JButton upJButton = null; private JButton downJButton = null; private JLabel upJLabel = null; private JLabel downJLabel = null; String[] items = { "不设置", "左边", "右边" }; public LogoSetPanel()
{
initUI();
initLister();
} /**
* 初始化界面
*/
private void initUI()
{ upJComboBox = new JComboBox(items);
downJComboBox = new JComboBox(items);
upJButton = new JButton("设置图片");
downJButton = new JButton("设置图片");
upJLabel = new JLabel(new ImageIcon("E:/1.PNG"));
downJLabel = new JLabel(new ImageIcon("E:/1.PNG"));
// 界面布局
this.setLayout(new java.awt.GridBagLayout());
this.add(upJComboBox, new GridBagConstraints(0, 0, 1, 1, 5, 5, GridBagConstraints.WEST,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
this.add(upJButton, new GridBagConstraints(0, 1, 1, 1, 5, 5, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 6, 0, 6), 0, 0));
this.add(upJLabel, new GridBagConstraints(0, 2, 1, 1, 10, 10, GridBagConstraints.EAST,
GridBagConstraints.BOTH, new Insets(0, 6, 0, 6), 0, 0));
this.add(downJComboBox, new GridBagConstraints(1, 0, 1, 1, 5, 5, GridBagConstraints.WEST,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
this.add(downJButton, new GridBagConstraints(1, 1, 1, 1, 5, 5, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 6, 0, 6), 0, 0));
this.add(downJLabel, new GridBagConstraints(1, 2, 1, 1, 10, 10, GridBagConstraints.EAST,
GridBagConstraints.BOTH, new Insets(0, 6, 0, 6), 0, 0)); }
}
可以用jbuilder中的xylayout布局
XYLayout.javaimport java.awt.*;
import java.io.Serializable;
import java.util.Hashtable;
public class XYLayout implements LayoutManager2,Serializable{
public XYLayout(){
info = new Hashtable();
}
public XYLayout(int width,int height){
info = new Hashtable();
this.width = width;
this.height = height;
}
public int getWidth(){
return width;
}
public void setWidth(int width){
this.width = width;
}
public int getHeight(){
return height;
}
public void setHeight(int height){
this.height = height;
}
public String toString(){
return String.valueOf(String.valueOf((new StringBuffer("XYLayout[width=")).append(width).append(",height=").append(
height).append("]")));
}
public void addLayoutComponent(String s,Component component1){}
public void removeLayoutComponent(Component component){
info.remove(component);
}
public Dimension preferredLayoutSize(Container target){
return getLayoutSize(target,true);
}
public Dimension minimumLayoutSize(Container target){
return getLayoutSize(target,false);
}
public void layoutContainer(Container target){
Insets insets = target.getInsets();
int count = target.getComponentCount();
for(int i = 0;i < count;i++){
Component component = target.getComponent(i);
if(component.isVisible()){
Rectangle r = getComponentBounds(component,true);
component.setBounds(insets.left + r.x,insets.top + r.y,r.width,r.height);
}
}
}
public void addLayoutComponent(Component component,Object constraints){
if(constraints instanceof XYConstraints) info.put(component,constraints);
}
public Dimension maximumLayoutSize(Container target){
return new Dimension(0x7fffffff,0x7fffffff);
}
public float getLayoutAlignmentX(Container target){
return 0.5F;
}
public float getLayoutAlignmentY(Container target){
return 0.5F;
}
public void invalidateLayout(Container container){}
Rectangle getComponentBounds(Component component,boolean doPreferred){
XYConstraints constraints = (XYConstraints) info.get(component);
if(constraints == null) constraints = defaultConstraints;
Rectangle r = new Rectangle(constraints.x,constraints.y,constraints.width,constraints.height);
if(r.width <= 0 || r.height <= 0){
Dimension d = doPreferred ? component.getPreferredSize() : component.getMinimumSize();
if(r.width <= 0) r.width = d.width;
if(r.height <= 0) r.height = d.height;
}
return r;
}
Dimension getLayoutSize(Container target,boolean doPreferred){
Dimension dim = new Dimension(0,0);
if(width <= 0 || height <= 0){
int count = target.getComponentCount();
for(int i = 0;i < count;i++){
Component component = target.getComponent(i);
if(component.isVisible()){
Rectangle r = getComponentBounds(component,doPreferred);
dim.width = Math.max(dim.width,r.x + r.width);
dim.height = Math.max(dim.height,r.y + r.height);
}
}
}
if(width > 0) dim.width = width;
if(height > 0) dim.height = height;
Insets insets = target.getInsets();
dim.width += insets.left + insets.right;
dim.height += insets.top + insets.bottom;
return dim;
}
private static final long serialVersionUID = 200L;
int width;
int height;
Hashtable info;
static final XYConstraints defaultConstraints = new XYConstraints();
}XYConstraints.javaimport java.io.Serializable;
public class XYConstraints implements Cloneable,Serializable{
private static final long serialVersionUID = -8277488378565252141L;
public XYConstraints(){
this(0,0,0,0);
}
public XYConstraints(int x,int y,int width,int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int getX(){
return x;
}
public void setX(int x){
this.x = x;
}
public int getY(){
return y;
}
public void setY(int y){
this.y = y;
}
public int getWidth(){
return width;
}
public void setWidth(int width){
this.width = width;
}
public int getHeight(){
return height;
}
public void setHeight(int height){
this.height = height;
}
public int hashCode(){
return x ^ y * 37 ^ width * 43 ^ height * 47;
}
public boolean equals(Object that){
if(that instanceof XYConstraints){
XYConstraints other = (XYConstraints) that;
return other.x == x && other.y == y && other.width == width && other.height == height;
}else{
return false;
}
}
public Object clone(){
return new XYConstraints(x,y,width,height);
}
public String toString(){
return String.valueOf(String.valueOf((new StringBuffer("XYConstraints[")).append(x).append(",").append(y).append(
",").append(width).append(",").append(height).append("]")));
}
int x;
int y;
int width;
int height;
}import javax.swing.*;public class LogoSetPanel extends JPanel{
// 上面的下拉框,可以选择logo图片在导出的excel中为左,或右或不选
private JComboBox upJComboBox = null;
// 下面的下拉框,可以选择logo图片在导出的excel中为左,或右或不选
private JComboBox downJComboBox = null;
// 设置logo的upButton和downButton
private JButton upJButton = null;
private JButton downJButton = null;
private JLabel upJLabel = null;
private JLabel downJLabel = null;
private XYLayout xyLayout = null;
String[] items = {"不设置","左边","右边"};
public LogoSetPanel(){
initUI();
// initLister();
}
/**
* 初始化界面
*/
private void initUI(){
xyLayout = new XYLayout();
this.setLayout(xyLayout);
upJComboBox = new JComboBox(items);
//设置upJComboBox,坐标0,0 宽50,高50
this.add(upJComboBox,new XYConstraints(0,0,50,50));
downJComboBox = new JComboBox(items);
upJButton = new JButton("设置图片");
//设置upJButton,坐标55,0 宽70,高50
this.add(upJButton,new XYConstraints(55,0,70,50));
downJButton = new JButton("设置图片");
upJLabel = new JLabel("这是个label,长度300,高50");
//设置upJLabel,坐标130,0 宽130,高50
this.add(upJLabel,new XYConstraints(130,0,300,50));
downJLabel = new JLabel(new ImageIcon("E:/1.PNG"));
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.add(new LogoSetPanel());
frame.pack();
frame.setVisible(true);
}
}
组件调用setBounds()方法设置位置;import javax.swing.*;public class LogoSetPanel extends JPanel{
// 上面的下拉框,可以选择logo图片在导出的excel中为左,或右或不选
private JComboBox upJComboBox = null;
// 下面的下拉框,可以选择logo图片在导出的excel中为左,或右或不选
private JComboBox downJComboBox = null;
// 设置logo的upButton和downButton
private JButton upJButton = null;
private JButton downJButton = null;
private JLabel upJLabel = null;
private JLabel downJLabel = null;
private XYLayout xyLayout = null;
String[] items = {"不设置","左边","右边"};
public LogoSetPanel(){
initUI();
// initLister();
}
/**
* 初始化界面
*/
private void initUI(){
this.setLayout(null);
upJComboBox = new JComboBox(items);
//设置upJComboBox,坐标0,0 宽50,高50
upJComboBox.setBounds(0,0,50,50);
this.add(upJComboBox);
upJButton = new JButton("设置图片");
//设置upJButton,坐标55,0 宽70,高50
upJButton.setBounds(55,0,70,50);
this.add(upJButton);
upJLabel = new JLabel("这是个label,长度300,高50");
//设置upJLabel,坐标130,0 宽130,高50
upJLabel.setBounds(130,0,300,50);
this.add(upJLabel);
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.add(new LogoSetPanel());
frame.setSize(600,300);
frame.setVisible(true);
}
}
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* @author closewubq
*/
public class GridBagLayoutDemo {
public static void addComponentsToPane(Container pane) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
button = new JButton("Button 1");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
button.setSize(100,100);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("Long-Named Button Long-Named Button 5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10, 0, 0, 0);
c.gridx = 1;
c.gridwidth = 3;
c.gridy = 2;
pane.add(button, c);
} private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
System.out.println(ClassLoader.getSystemResource(""));
System.out.println(FileTest.class.getResource("")); //Test为你自己的类文件
System.out.println(FileTest.class.getResource("/")); //Class文件所在路径
System.out.println(new File("/").getAbsolutePath());
System.out.println(System.getProperty("user.dir"));