- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先很抱歉问这个问题,我对java完全陌生,真的需要一些指导。假设我想制作这样的 Java GUI
想让标题背景具有响应性,所以我将背景图像分成两部分,棕色部分和橙色部分,我想要实现的是使棕色部分可调整大小。
@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 看起来一团糟,我无法正确放置这两个部分,这就是它的样子..
在网上查了好几天了,还是没有找到答案。
最佳答案
i have searching on the internet for a few days and still have not found the answer yet.
输入 JLabel与 Icon/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.
从代码来看,需要对齐(水平和垂直)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/
我在 Web 应用程序中尝试了一些字节码操作,到目前为止,效果很好。现在我需要在代码中的某些特定位置注入(inject)一些字节代码。我用 switch 语句和 method.inserAt(); 尝
我正在尝试对数组中的对象值求和,然后计算每个值相对于总数的百分比。 这里是数组: [ { "ratiototale": [ [ { "0":
我在接口(interface)中有以下方法.. Task> SearchAsync(TU searchOptions); 效果很好。 现在我正在尝试进行单元测试以测试何时出现问题 - 代码会抛出异常。
我似乎无法让 reveal 开始工作,我已经严格遵守所有内容,添加 modernizr,添加插件,添加 css(reveal 显然包含在 foundation 中),在开始时初始化 foundatio
我正在尝试移植 raspberrypi's userspace 的位从 C 到 golang 的代码,我遇到了一个涉及 ioctl() 的程序。 我在执行 C 代码时遇到了问题 #define MAJ
我一直在尝试用纯 Java 制作一个简单的游戏,但在绘图时遇到了问题。我正在尝试保持相对较高的帧速率,但是 JFrame.repaint() 不能被“强制”并且只是在下一个可用机会重绘帧的请求这一事实
给定一个字符串 "2*(i+j) = 20 我想返回 ((lambda x: x >= 20), ['i']) 我可以然后直接提供给 constraint。 最佳答案 您正在寻找 eval 的替代方法
我对改进我的 javascript 代码以使其成为正确的 OOP 很感兴趣....目前我倾向于做这样的事情: jQuery(document).ready(function () { Page
我有一个调用两个动画 Action 的事件监听器。不幸的是,它们的开始有少量错开(例如,函数中的第一个首先开始)。 有谁知道正确同步它们的方法吗? 这是我的代码: $("#nav ul li a").
我只需要检查目录是否存在!但是,如果目录是“E:\Test”,其中 E: 是 CD/DVD 驱动器,并且上面没有插入磁盘,我会看到以下 Delphi 和 Windows 问题。 第一种方法: func
同样的问题: https://stackoverflow.com/questions/11294207/exchange-web-services-argumentexception-using-my
如果您跳转到 this question 中的第一个答案你会看到他使用 Employee.prototype = new Person(); 将 Person.prototype 继承到 Employ
我需要知道如何正确地遍历元素的 ArrayList 并计算元素在列表中出现的次数,而无需事先知道该元素。我尝试了几种方法并且有一种目前有效,但我觉得它很丑陋且不合适。 为了更深入地解释,我有一个 ja
我有一个用 Python 编写的(非常基本但工作完美的)AWS lambda 函数,但是它具有嵌入式凭证以连接到:1)外部网络服务2) DynamoDB 表。 该函数的作用相当基本:它针对服务发布登录
我很好奇 Tornado 推荐的查询 Redis(或任何数据库)的方法是什么。 我见过一些像 https://gist.github.com/357306 这样的例子但他们似乎都在使用对 redis
这更像是一个“我做得对吗”的问题。 快速背景故事:我已经构建了一个 gradle 插件(在一个独立的 gradle/groovy 项目中)。我在另一个 java 项目中使用它。客户项目通过以下方式引用
在我的代码中,我有: function handleMessage() { const twilio = require('twilio')(process.env.TWILIO_ACCOUNT_
我正在努力在 Linux 中刷新嵌入式设备的先前 ROM 转储。我以前的转储包含 oob 数据。我是用nandwrite -n -N -o/dev/mtd0 backup.bin写的,然后再做一次RO
我正在尝试使用 go 为 react-router 提供服务,我已经做到了,但我遇到了麻烦,我认为我做的方式不正确,或者不完整。我正在使用 Mux。我遇到的麻烦是当我按下 时在我的应用程序中,它正确
我正在尝试 promise JSON.parse 方法,但不幸的是没有任何运气。这是我的尝试: Promise.promisify(JSON.parse, JSON)(data).then((resu
我是一名优秀的程序员,十分优秀!