gpt4 book ai didi

java - 如何正确地将一些java swing控件放在右上角?

转载 作者:行者123 更新时间:2023-12-01 23:29:37 25 4
gpt4 key购买 nike

首先很抱歉问这个问题,我对java完全陌生,真的需要一些指导。假设我想制作这样的 Java GUI

enter image description here

想让标题背景具有响应性,所以我将背景图像分成两部分,棕色部分和橙色部分,我想要实现的是使棕色部分可调整大小。

    @SuppressWarnings("serial")
class ImagePanel extends JPanel
{
private Image image;
private boolean tile;

ImagePanel(Image image) throws IOException
{
this.image = image;
this.tile = false;
};

@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (tile)
{
int iw = image.getWidth(this);
int ih = image.getHeight(this);
if (iw > 0 && ih > 0)
{
for (int x = 0; x < getWidth(); x += iw)
{
for (int y = 0; y < getHeight(); y += ih)
{
g.drawImage(image, x, y, iw, ih, this);
}
}
}
}
else
{
g.drawImage(image, 0, 0, getWidth(), 65, this);

}
}
}

public class FileListing
{
public Component getGui(File[] all, boolean vertical)
{
// put File objects in the list..
fileList1 = new JList(all);
fileList1.setFixedCellWidth(150);

fileList1.addListSelectionListener(new HtmlListing());

// ..then use a renderer
fileList1.setCellRenderer(new FileRenderer(!vertical));

if (!vertical)
{
fileList1.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
fileList1.setVisibleRowCount(-1);
}
else
{
fileList1.setVisibleRowCount(9);
}
return new JScrollPane(fileList1);
}

static ArrayList<File> globarr = null;
static JList fileList1 = null;
static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
static JTable table = null;
static JPanel gui = null;
static JPanel CentCont = null;
static JPanel EastCont = null;

static class HtmlListing implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent event)
{
if (!event.getValueIsAdjusting())
{
globarr = new ArrayList<File>();

FileListing fl = new FileListing();
fl.walk(fileList1.getSelectedValue() + "work\\airasia\\html", 500, 0);

if(globarr.size() > 0)
{
Object[][] data = new Object[globarr.size()][globarr.size()];

for(int i = 0; i < globarr.size(); i++)
{
if(globarr.get(i).isFile())
{
//tes[i] = (File)globarr.get(i);
String filename = globarr.get(i).getName().toString();
String date = sdf.format(globarr.get(i).lastModified());

Object[] obj = new Object[] {filename, filename.substring(filename.lastIndexOf(".") + 1), date, globarr.get(i).getAbsolutePath()};
data[i] = obj;
}
}

Object[] column = new Object[]{"name ", "type", "date modified", "path"};




DefaultTableModel model = new DefaultTableModel(data, column);

table = new JTable(model)
{
private static final long serialVersionUID = 1L;

public boolean isCellEditable(int row, int column)
{
return false;
};
};


table.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
int rowIdx = table.getSelectedRow(); // path to your new file
TableModel tm = table.getModel();
String path = tm.getValueAt(rowIdx, 3).toString();
File htmlFile = new File(path);

try // open the default web browser for the HTML page
{
Desktop.getDesktop().browse(htmlFile.toURI());
//Desktop.getDesktop().open(htmlFile);
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});

table.removeColumn(table.getColumnModel().getColumn(3));

table.setFillsViewportHeight(true);
table.setIntercellSpacing(new Dimension(0, 0));
table.setShowGrid(false);


JScrollPane scrollPane = new JScrollPane(table);

EastCont = new JPanel();
EastCont.setLayout(new BorderLayout());
EastCont.add(scrollPane);
EastCont.setPreferredSize(new Dimension(1100, 0));

gui.add(EastCont, BorderLayout.EAST);
gui.revalidate();
gui.repaint();
}
else
{
//CentCont.remove(comp);
EastCont.remove(table);
gui.remove(EastCont);
gui.revalidate();
gui.repaint();
}
}
}
}

int idx = 0;

public void walk( String path, int length, int loopidx )
{
File root = new File( path );
File[] list = root.listFiles();

if (list == null) return ;

for ( File f : list )
{
if ( f.isDirectory() )
{
walk( f.getAbsolutePath(), 0, idx);
}
else
{
if(f.getName().endsWith(".html"))
{
globarr.add(f);
idx += 1;
}
}
}
}

public static void main(String[] args) throws IOException
{

JFrame frame = new JFrame("File List");
gui = new JPanel(new BorderLayout());

JButton btnScan = new JButton("SCAN");
//btnScan.setPreferredSize(new Dimension(70, 40));
btnScan.setBounds(0, 0, 200, 100);

btnScan.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
File[] f = File.listRoots();
FileListing fl = new FileListing();

Component c = fl.getGui(f,true);
CentCont.add(c);
CentCont.setBorder(new EmptyBorder(3,3,3,3));
CentCont.setPreferredSize(new Dimension(50, 100));

}
});


Image image = ImageIO.read(new File("C:\\Users\\User\\Desktop\\header-coklat_01_01.jpg"));
JPanel imagePanel = new ImagePanel(image);

JLabel lbl = new JLabel();
ImageIcon imgThisImg = new ImageIcon(ImageIO.read(new File("C:\\Users\\User\\Desktop\\dashboard_02.jpg")));
lbl.setIcon(imgThisImg);


JPanel anotherPanel = new JPanel(); /// multiple panel,
anotherPanel.setSize(1000, 0);
anotherPanel.setOpaque(false); // THIS IS VERY MUCH IMPORTANT
anotherPanel.add(lbl);

ImagePanel tes = new ImagePanel(image);
tes.add(imagePanel, BorderLayout.WEST);
tes.add(anotherPanel, BorderLayout.EAST);

CentCont = new JPanel();
CentCont.setLayout(new BorderLayout());

JButton btnPreview = new JButton("Preview");
btnPreview.setBounds(25, 25, 200, 100);
//btnPreview.setPreferredSize(new Dimension(70, 40));
btnPreview.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int rowIdx = table.getSelectedRow(); // path to your new file
TableModel tm = table.getModel();
String path = tm.getValueAt(rowIdx, 3).toString();
File htmlFile = new File(path);

// open the default web browser for the HTML page
try
{
Desktop.getDesktop().browse(htmlFile.toURI());
//Desktop.getDesktop().open(htmlFile);

}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

JPanel WestCont = new JPanel();
WestCont.setLayout(new BorderLayout());
WestCont.add(btnScan, BorderLayout.NORTH);
WestCont.add(btnPreview, BorderLayout.CENTER);

//gui.setPreferredSize(new Dimension(100, 600));
gui.add(WestCont, BorderLayout.WEST);

//CentCont.add(btnPreview, BorderLayout.SOUTH);
gui.add(CentCont, BorderLayout.CENTER);
gui.add(tes, BorderLayout.NORTH);

frame.setContentPane(gui);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.pack();
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}


}

到目前为止我已经尝试过这个,但是 GUI 看起来一团糟,我无法正确放置这两个部分,这就是它的样子..

enter image description here

在网上查了好几天了,还是没有找到答案。

最佳答案

i have searching on the internet for a few days and still have not found the answer yet.

  • 输入 JLabelIcon/ImageIcon

  • JPanel ,将布局管理器更改为 FlowLayout.RIGHT (默认布局管理器是FlowLayout)

  • 上午JPanel 放置到 JFrame.NORTH 区域(默认布局管理器是 BorderLayout )

  • Dimension(我的上述三点)是根据使用的 LayoutManager 通过 Icon/ImageIcon 的像素大小计算得出的

编辑

i already did point 1 to 3 but not showing any difference.

  • 不正确

enter image description here

从代码来看,需要对齐(水平和垂直)Icon、IconGap,更多信息请参见JLabel API

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class FlowLayoutRight {

private JFrame frame = new JFrame();
private JPanel panelNorth = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 50);
}
};
private Icon icon = UIManager.getIcon("OptionPane.errorIcon");
private JLabel label = new JLabel(icon);
private JPanel panelCenter = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
};

public FlowLayoutRight() {
panelNorth.setLayout(new FlowLayout(FlowLayout.RIGHT));
panelNorth.add(label);
panelNorth.setBackground(Color.BLUE);
panelCenter.setBackground(Color.ORANGE);
frame.add(panelNorth, BorderLayout.NORTH);
frame.add(panelCenter);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(100, 100);
frame.setVisible(true);
}

public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new FlowLayoutRight();
}
});
}
}

关于java - 如何正确地将一些java swing控件放在右上角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19540380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com