1个回答
展开全部
按照你的要求编写的Java程序如下
VFlowLayout.java(用到的垂直流布局文件)
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
public class VFlowLayout extends FlowLayout{
private static final long serialVersionUID = 1L;
public static final int TOP = 0;
public static final int MIDDLE = 1;
public static final int BOTTOM = 2;
int hgap;
int vgap;
boolean hfill;
boolean vfill;
public VFlowLayout(){
this(TOP, 5, 5, true, false);
}
public VFlowLayout(boolean hfill, boolean vfill){
this(TOP, 5, 5, hfill, vfill);
}
public VFlowLayout(int align){
this(align, 5, 5, true, false);
}
public VFlowLayout(int align, boolean hfill, boolean vfill){
this(align, 5, 5, hfill, vfill);
}
public VFlowLayout(int align, int hgap, int vgap, boolean hfill, boolean vfill){
setAlignment(align);
this.hgap = hgap;
this.vgap = vgap;
this.hfill = hfill;
this.vfill = vfill;
}
public Dimension preferredLayoutSize(Container target){
Dimension tarsiz = new Dimension(0, 0);
for (int i = 0; i < target.getComponentCount(); i++){
Component m = target.getComponent(i);
if (m.isVisible()){
Dimension d = m.getPreferredSize();
tarsiz.width = Math.max(tarsiz.width, d.width);
if (i > 0){
tarsiz.height += hgap;
}
tarsiz.height += d.height;
}
}
Insets insets = target.getInsets();
tarsiz.width += insets.left + insets.right + hgap * 2;
tarsiz.height += insets.top + insets.bottom + vgap * 2;
return tarsiz;
}
public Dimension minimumLayoutSize(Container target){
Dimension tarsiz = new Dimension(0, 0);
for (int i = 0; i < target.getComponentCount(); i++){
Component m = target.getComponent(i);
if (m.isVisible()){
Dimension d = m.getMinimumSize();
tarsiz.width = Math.max(tarsiz.width, d.width);
if (i > 0){
tarsiz.height += vgap;
}
tarsiz.height += d.height;
}
}
Insets insets = target.getInsets();
tarsiz.width += insets.left + insets.right + hgap * 2;
tarsiz.height += insets.top + insets.bottom + vgap * 2;
return tarsiz;
}
public void setVerticalFill(boolean vfill){
this.vfill = vfill;
}
public boolean getVerticalFill(){
return vfill;
}
public void setHorizontalFill(boolean hfill){
this.hfill = hfill;
}
public boolean getHorizontalFill(){
return hfill;
}
private void placethem(Container target, int x, int y, int width, int height, int first, int last){
int align = getAlignment();
if (align == MIDDLE){
y += height / 2;
}
if (align == BOTTOM){
y += height;
}
for (int i = first; i < last; i++){
Component m = target.getComponent(i);
Dimension md = m.getSize();
if (m.isVisible()){
int px = x + (width - md.width) / 2;
m.setLocation(px, y);
y += vgap + md.height;
}
}
}
public void layoutContainer(Container target){
Insets insets = target.getInsets();
int maxheight = target.getSize().height - (insets.top + insets.bottom + vgap * 2);
int maxwidth = target.getSize().width - (insets.left + insets.right + hgap * 2);
int numcomp = target.getComponentCount();
int x = insets.left + hgap, y = 0;
int colw = 0, start = 0;
for (int i = 0; i < numcomp; i++){
Component m = target.getComponent(i);
if (m.isVisible()){
Dimension d = m.getPreferredSize();
if ((this.vfill) && (i == (numcomp - 1))){
d.height = Math.max((maxheight - y), m.getPreferredSize().height);
}
if (this.hfill){
m.setSize(maxwidth, d.height);
d.width = maxwidth;
}else{
m.setSize(d.width, d.height);
}
if (y + d.height > maxheight){
placethem(target, x, insets.top + vgap, colw, maxheight - y, start, i);
y = d.height;
x += hgap + colw;
colw = d.width;
start = i;
}else{
if (y > 0){
y += vgap;
}
y += d.height;
colw = Math.max(colw, d.width);
}
}
}
placethem(target, x, insets.top + vgap, colw, maxheight - y, start, numcomp);
}
}
C.java(主程序)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
class MyDialog extends JDialog implements ActionListener{
JPanel jp=new JPanel();
TitledBorder tb=new TitledBorder("预览");
JButton []jb=new JButton[9];
JLabel jl=new JLabel("单击下方图示或使用按钮可",JLabel.CENTER);
JLabel jl2=new JLabel("应用边框",JLabel.CENTER);
JPanel jp1=new JPanel();
JPanel jp3=new JPanel();
JPanel jp4=new JPanel();
JPanel jp5=new JPanel();
JPanel jp6=new JPanel();
JPanel jp7=new JPanel();
JPanel jp8=new JPanel();
JPanel jp9=new JPanel();
JPanel jp2=new JPanel();
JLabel jl1=new JLabel("应用于(L):");
JComboBox<String> jcb=new JComboBox<String>();
JButton jb1=new JButton("选项...");
ImageIcon ii1=new ImageIcon("male.png");
ImageIcon ii2=new ImageIcon("female.png");
ImageIcon ii3=new ImageIcon();
BufferedImage image = new BufferedImage(50,50,BufferedImage.TYPE_INT_RGB);
MyDialog(){
setTitle("预览");
Graphics2D g2d = image.createGraphics();
image = g2d.getDeviceConfiguration().createCompatibleImage(50, 50, Transparency.TRANSLUCENT);
g2d.dispose();
ii1.setImage(ii1.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));
ii2.setImage(ii2.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));
ii3.setImage(image.getScaledInstance(50,50,Image.SCALE_DEFAULT));
for(int i=0;i<9;i++){
jb[i]=new JButton();
jb[i].setOpaque(false);
jb[i].setContentAreaFilled(false);
jb[i].setMargin(new Insets(0, 0, 0, 0));
jb[i].setFocusPainted(false);
jb[i].setBorderPainted(false);
jb[i].setBorder(null);
jb[i].addActionListener(this);
}
jb[0].setIcon(ii1);jb[1].setIcon(ii1);jb[2].setIcon(ii1);
jb[4].setIcon(ii1);jb[5].setIcon(ii1);jb[6].setIcon(ii1);
jb[3].setIcon(ii2);jb[7].setIcon(ii2);jb[8].setIcon(ii3);
jp3.setLayout(new VFlowLayout(VFlowLayout.MIDDLE));
jp3.add(jb[0]);jp3.add(jb[1]);jp3.add(jb[2]);
jp4.setLayout(new BorderLayout());
jp5.add(jb[4]);jp5.add(jb[5]);jp5.add(jb[6]);
jp7.add(jb[8]);
jp4.add(jb[3],BorderLayout.WEST);
jp4.add(jb[7],BorderLayout.EAST);
jp4.add(jp5,BorderLayout.CENTER);
jp8.setLayout(new GridLayout(2,1));
jp8.add(jl);jp8.add(jl2);
jp1.setLayout(new BorderLayout());
jp1.add(jp8,BorderLayout.NORTH);
jp1.add(jp7,BorderLayout.EAST);
jp1.add(jp3,BorderLayout.WEST);
jp1.add(jp6,BorderLayout.CENTER);
jp1.add(jp4,BorderLayout.SOUTH);
jcb.addItem("表格");
jp2.setLayout(new GridLayout(3,1));
jp9.setLayout(new FlowLayout(FlowLayout.RIGHT));
jp9.add(jb1);
jp2.add(jl1);jp2.add(jcb);jp2.add(jp9);
jp.setLayout(new BorderLayout());
jp.add(jp1,BorderLayout.CENTER);
jp.add(jp2,BorderLayout.SOUTH);
jp.setBorder(tb);
add(jp,BorderLayout.CENTER);
setSize(500, 500);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setModal(true);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb[0]){
jp6.setBackground(Color.RED);
}
if(e.getSource()==jb[1]){
jp6.setBackground(Color.ORANGE);
}
if(e.getSource()==jb[2]){
jp6.setBackground(Color.YELLOW);
}
if(e.getSource()==jb[4]){
jp6.setBackground(Color.GREEN);
}
if(e.getSource()==jb[5]){
jp6.setBackground(Color.CYAN);
}
if(e.getSource()==jb[6]){
jp6.setBackground(Color.BLUE);
}
}
}
public class C extends JFrame implements ActionListener{
JButton jb=new JButton("Open Dialog");
C(){
this.setLayout(new FlowLayout());
this.add(jb);
jb.addActionListener(this);
this.setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new C();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb){
MyDialog md=new MyDialog();
}
}
}
运行结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询