weixin_43472711 2018-10-20 07:39 采纳率: 0%
浏览 1446

为什么对容器用了绝对布局之后组件显示不出来,取消容器绝对布局而对窗体用绝对布局就显示出来

setbounds()函数也没起到作用,按钮还是一字排开
package handsomeboy;
import org.apache.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import java.awt.Point;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class PanelTranslate{
static Point origin = new Point();
static JFrame f = new JFrame();

JPanel FilePanel = new JPanel();/*第一个装“打开文件的容器”*/
static JPanel CalulatePanel = new JPanel();/*第二个装“相似度计算算法的”容器*/
static TextField FileDictory = new TextField(50);
static Icon picture_1 = new ImageIcon("C:\Users\lenovo\Downloads\8435e5dde71190ef8a3c29d7ce1b9d16fcfa60f7.jpg");
static Icon picture_2 = new ImageIcon("C:\Users\lenovo\Downloads\b03533fa828ba61edcfdc60d4334970a304e5984.jpg");
static Icon picture_3 = new ImageIcon("C:\\Users\\lenovo\\Downloads\\b2de9c82d158ccbfe522988411d8bc3eb03541f5.jpg");
static JButton confirm = new JButton("确定");
static JButton FileImport = new JButton("文件导入");
static JButton Cosine = new JButton("余弦算法",picture_1);
static JButton PC = new JButton("皮尔逊相关性算法",picture_2);
static JButton Spearman = new JButton("斯皮尔曼算法",picture_3);
static JButton colse = new JButton("关闭");
FileDialog d1 = new FileDialog(f,"选择需要打开文件",FileDialog.LOAD);
FileDialog d2 = new FileDialog(f, "保存文件路径", FileDialog.SAVE);
Point pressedPoint;
public void init() {

    CalulatePanel.add(Cosine);
    CalulatePanel.add(PC);
    CalulatePanel.add(Spearman);
    Cosine.setVerticalTextPosition(JButton.BOTTOM);
    Cosine.setHorizontalTextPosition(JButton.CENTER);
    Cosine.setMargin(new Insets(0,0,0,0));//将边框外的上下左右空间设置为0 
    Cosine.setBorderPainted(false);
    Cosine.setIconTextGap(0);//将标签中显示的文本和图标之间的间隔量设置为0  
    Cosine.setBorderPainted(false);//不打印边框  
    Cosine.setBorder(null);//除去边框 
    Cosine.setContentAreaFilled(false);
    Cosine.setFont(new java.awt.Font("华文行楷", 1, 30));
    PC.setVerticalTextPosition(JButton.BOTTOM);
    PC.setHorizontalTextPosition(JButton.CENTER);
    PC.setBorderPainted(false);
    PC.setIconTextGap(0);//将标签中显示的文本和图标之间的间隔量设置为0  
    PC.setBorderPainted(false);//不打印边框  
    PC.setBorder(null);//除去边框 
    PC.setContentAreaFilled(false);
    PC.setFont(new java.awt.Font("华文行楷", 1, 30));
    Spearman.setVerticalTextPosition(JButton.BOTTOM);
    Spearman.setHorizontalTextPosition(JButton.CENTER);
    Spearman.setBorderPainted(false);
    Spearman.setIconTextGap(0);//将标签中显示的文本和图标之间的间隔量设置为0  
    Spearman.setBorderPainted(false);//不打印边框  
    Spearman.setBorder(null);//除去边框 
    Spearman.setContentAreaFilled(false);
    Spearman.setFont(new java.awt.Font("华文行楷", 1, 30));
    f.getContentPane().setBackground(new Color(195, 184, 162)); // 设置窗体背景颜色
    f.setUndecorated(true);// 取消窗体修饰效果
    f.getContentPane().setLayout(null);// 窗体使用绝对布局
    f.setLocationRelativeTo(null);// 窗体居中
    f.setAlwaysOnTop(true); //窗体最顶层显示
    f.setSize(1000, 800);

    FileDictory.setBounds(50, 50, 100,100);
    FileImport.setBounds(100, 50, 100, 100);
    confirm.setBounds(100, 50, 100, 100);
    FilePanel.add(FileDictory);
    FilePanel.add(FileImport);
    FilePanel.add(confirm);
    JPanel bgPanel = new JPanel()
    {
        protected void paintComponent(Graphics g)
        {
            try {
                Image bg = ImageIO.read(new File("C:\\Users\\lenovo\\Downloads\\537450_223029793000_2.jpg"));
                g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    f.setContentPane(bgPanel);
    f.add(FilePanel);
    FilePanel.setVisible(true);
    CalulatePanel.setVisible(false);
    f.setVisible(true);

    colse.addActionListener(new ActionListener() {// 设置按钮关闭动作事件处理
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    confirm.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            JPanel bgPanel = new JPanel()
            {
                protected void paintComponent(Graphics g)
                {
                    try {
                        Image bg = ImageIO.read(new File("C:\\Users\\lenovo\\Downloads\\2367.jpg_wh1200.jpg"));
                        g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            f.setContentPane(bgPanel);
            f.add(CalulatePanel);
            FilePanel.setVisible(false);
            CalulatePanel.setVisible(true);
        }

    });
    f.addMouseListener(new MouseAdapter() {
        // 按下(mousePressed 不是点击,而是鼠标被按下没有抬起)
        public void mousePressed(MouseEvent e) {
        // 当鼠标按下的时候获得窗口当前的位置
        origin.x = e.getX();
        origin.y = e.getY();
        }
        });
        f.addMouseMotionListener(new MouseMotionAdapter() {
        // 拖动(mouseDragged 指的不是鼠标在窗口中移动,而是用鼠标拖动)
        public void mouseDragged(MouseEvent e) {
        // 当鼠标拖动时获取窗口当前位置
        Point p = f.getLocation();
        // 设置窗口的位置
        // 窗口当前的位置 + 鼠标当前在窗口的位置 - 鼠标按下的时候在窗口的位置
        f.setLocation(p.x + e.getX() - origin.x, p.y + e.getY()- origin.y);
        }
        });
}
public static void main(String[] args) {
    new PanelTranslate().init();
}

}

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2018-10-27 12:19
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题