- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个窗口,其中包含两个垂直比例为 3:1 的面板,并让较大的窗口包含一个方形面板。另外,我想在方形面板的中心画一些东西,比如圆圈。
我的程序的行为表明 3:1 比例和方形之间存在冲突。调整大小时,形状会奇怪地跳跃。最终结果是错误的,第一个比例不成立。此外,圆偏离中心。
我是java新手,因此我希望能对错误以及如何以正确的方式实现这一点进行评论。
结果如下:
我想要:(1) 红色面板为正方形 (2) 蓝色面板严格比绿色面板高 3 倍 (3) 在红色框中间有圆圈。
这是我的代码:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ProportionalPanels
{
private JFrame window = new JFrame("Proportional resizable panels");
private JPanel allContaining = new JPanel( new GridBagLayout() );
private JPanel upper = new JPanel( new GridBagLayout() );
private JPanel lower = new JPanel();
private JPanel square = new JPanel()
{
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
Container c = getParent();
if (c != null) {
d = c.getSize();
} else {
return new Dimension(10, 10);
}
int w = (int) d.getWidth();
int h = (int) d.getHeight();
int s = (w < h ? w : h);
return new Dimension(s, s);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
int x = this.getWidth(), y = this.getHeight();
g.fillOval(x/2,y/2,x/4,y/4);
}
};
public ProportionalPanels()
{
// setting colors
allContaining.setBackground(Color.WHITE);
square.setBackground(Color.RED);
upper.setBackground(Color.BLUE);
lower.setBackground(Color.GREEN);
// setting upper
upper.add(square);
// setting allContaining
GridBagConstraints g = new GridBagConstraints();
g.gridx = g.gridy = 0;
g.weightx = 1; g.weighty = 0.75;
g.fill = GridBagConstraints.BOTH;
allContaining.add(upper,g);
g.gridy += 1;
g.weighty = 0.25;
g.fill = GridBagConstraints.BOTH;
allContaining.add(lower,g);
window.add(allContaining);
// setting window
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
window.setLocationByPlatform(true);
window.pack();
window.setSize(400,200);
window.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater( new Runnable()
{
public void run()
{
new ProportionalPanels();
}
});
}
}
想象一个简单的国际象棋图形用户界面,只是为了类比。一个窗口被分成两个面板,上面的一个(蓝色)是包含棋盘的,下面的一个(绿色)是一些按钮。这些面板的唯一限制是保持垂直比例 - 上面的面板比下面的面板高 3 倍。然后,深入到面板层次结构中,上面板包含 square
面板(红色),它是一个正方形,具有上面板的高度,并且水平放置在上面板的中间。方形面板的左侧和右侧有额外的空间,相应调整以保持红色面板的方形,绿色的高度比红色的小 3 倍。红色面板的中心有一些图画,这里是一个灰色圆圈,但为了保持国际象棋的类比,它可以是棋盘或其他任何东西的图像。
最佳答案
我不是 100% 确定我理解这个问题,但是,它以前从未阻止过我......
基本上,这使用了一个非常粗糙的自定义布局管理器。它将保持第一个和最后一个组件的尺寸为 3:1 比例(宽度是高度的比率)
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LayoutManager;
import java.awt.LayoutManager2;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JPanel master = new JPanel(new PropertionalLayoutManager());
master.add(new TestPane(), "left");
master.add(new DrawPane(), "center");
master.add(new TestPane(), "right");
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(master);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PropertionalLayoutManager implements LayoutManager {
private Component left;
private Component right;
private Component center;
@Override
public void addLayoutComponent(String name, Component comp) {
if ("center".equals(name)) {
center = comp;
} else if ("left".equals(name)) {
left = comp;
} else if ("right".equals(name)) {
right = comp;
}
}
@Override
public void removeLayoutComponent(Component comp) {
}
public Dimension getSize(Dimension leftSize, Dimension centerSize, Dimension rightSize) {
Dimension size = new Dimension();
if (leftSize != null && right != null) {
int width = leftSize.width;
int height = leftSize.height;
size.width = Math.max(width, rightSize.width) * 2;
size.height = Math.max(height, rightSize.height);
} else if (leftSize != null) {
size.width = leftSize.width;
size.height = leftSize.height;
} else if (right != null) {
size.width = rightSize.width;
size.height = rightSize.height;
}
if (center != null) {
size.width += centerSize.width;
size.height = Math.max(centerSize.height, size.height);
}
return size;
}
@Override
public Dimension preferredLayoutSize(Container parent) {
Dimension leftSize = left == null ? null : left.getPreferredSize();
Dimension centerSize = right == null ? null : right.getPreferredSize();
Dimension rightSize = center == null ? null : center.getPreferredSize();
return getSize(leftSize, centerSize, rightSize);
}
@Override
public Dimension minimumLayoutSize(Container parent) {
Dimension leftSize = left == null ? null : left.getMinimumSize();
Dimension centerSize = right == null ? null : right.getMinimumSize();
Dimension rightSize = center == null ? null : center.getMinimumSize();
return getSize(leftSize, centerSize, rightSize);
}
@Override
public void layoutContainer(Container parent) {
// Get rid of anything else that might have been added...
for (Component comp : parent.getComponents()) {
comp.setBounds(0, 0, 0, 0);
}
int width = parent.getWidth();
int height = parent.getHeight();
int outterWidth = height / (3 / 1);
if (left != null) {
left.setBounds(0, 0, outterWidth, height);
}
if (right != null) {
right.setBounds(width - outterWidth, 0, outterWidth, height);
}
if (center != null) {
center.setBounds(outterWidth, 0, width - (outterWidth * 2), height);
}
}
}
public class DrawPane extends JPanel {
public DrawPane() {
setBackground(Color.BLUE);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 300);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (getWidth() - (getWidth() / 4)) / 2;
int y = (getHeight()- (getHeight() / 4)) / 2;
g.setColor(Color.GRAY);
g.fillOval(x, y, getWidth() / 4, getHeight() / 4);
}
}
public class TestPane extends JPanel {
private JLabel size;
public TestPane() {
setBackground(Color.GREEN);
setLayout(new GridBagLayout());
size = new JLabel();
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
size.setText(getWidth() + "x" + getHeight());
}
});
add(size);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 300);
}
}
}
已更新
更像是......
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JPanel master = new JPanel(new SquareLayoutManager());
master.setBackground(Color.BLUE);
master.add(new DrawPane());
JPanel green = new JPanel();
green.setBackground(Color.GREEN);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 0.66666666666667;
gbc.fill = GridBagConstraints.BOTH;
frame.add(master, gbc);
gbc.gridy++;
gbc.weighty = 0.33333333333333;
frame.add(green, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class SquareLayoutManager implements LayoutManager {
@Override
public void addLayoutComponent(String name, Component comp) {
}
@Override
public void removeLayoutComponent(Component comp) {
}
@Override
public Dimension preferredLayoutSize(Container parent) {
int width = 0;
int height = 0;
for (Component comp : parent.getComponents()) {
width = Math.max(comp.getPreferredSize().width, width);
height = Math.max(comp.getPreferredSize().width, height);
}
// You could define rows and columns and blah, blah, blah, but I'm to lazy...
return new Dimension(Math.max(width, height) * parent.getComponentCount(), Math.max(width, height));
}
@Override
public Dimension minimumLayoutSize(Container parent) {
return preferredLayoutSize(parent);
}
@Override
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
int size = (parent.getHeight() - (insets.bottom + insets.top));
int x = (parent.getWidth() - (insets.left + insets.right) - size) / 2;
int y = insets.top;
for (Component comp : parent.getComponents()) {
comp.setBounds(x, y, size, size);
x += size; // Could define a gap, but, lazy...
}
}
}
public class DrawPane extends JPanel {
public DrawPane() {
setBackground(Color.RED);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 300);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (getWidth() - (getWidth() / 4)) / 2;
int y = (getHeight() - (getHeight() / 4)) / 2;
g.setColor(Color.GRAY);
g.fillOval(x, y, getWidth() / 4, getHeight() / 4);
}
}
}
关于java - 带 3 个比例面板的可调整大小的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26922807/
https://github.com/mattdiamond/Recorderjs/blob/master/recorder.js中的代码 我不明白 JavaScript 语法,比如 (functio
在 iOS 7 及更早版本中,如果我们想在应用程序中找到 topMostWindow,我们通常使用以下代码行 [[[UIApplication sharedApplication] windows]
我已经尝试解决这个问题很长一段时间了:我无法访问窗口的 url,因为它位于另一个域上..有一些解决方案吗? function login() { var cb = window.ope
是否可以将 FFMPEG 视频流传递到 C# 窗口?现在它在新窗口中作为新进程打开,我只是想将它传递给我自己的 SessionWindow。 此时我像这样执行ffplay: public void E
我有一个名为 x 的矩阵看起来像这样: pTime Close 1 1275087600 1.2268 2 1275264000 1.2264 3 1275264300 1.2
在编译时,发生搜索,grep搜索等,Emacs会在单独的窗口中创建一个新的缓冲区来显示结果,有没有自动跳转到那个窗口的方法?这很有用,因为我可以使用 n 和 p 而不是 M-g n 和 M-g p 移
我有一个启动 PowerShell 脚本的批处理文件。 批处理文件: START Powershell -executionpolicy RemoteSigned -noexit -file "MyS
我有一个基于菜单栏的应用程序,单击图标时会显示一个窗口。在 Mac OS X Lion 上一切正常,但由于某种原因,在 Snow Leopard 和早期版本的 Mac OS X 上会出现错误。任何时候
在 macOS 中,如何在 Xcode 和/或 Interface Builder 中创建带有“集成标题栏和工具栏”的窗口? 这是“宽标题栏”类型的窗口,已添加到 OS X 10.10 Yosemit
在浏览器 (Chrome) 中 JavaScript: var DataModler = { Data: { Something: 'value' }, Process: functi
我有 3 个 html 页面。第 1 页链接到第 2 页,第 2 页链接到第 3 页(为了简单起见)。 我希望页面 2 中的链接打开页面 3 并关闭页面 1(选项卡 1)。 据我了解,您无法使用 Ja
当点击“创建节点”按钮时,如何打开一个新的框架或窗口?我希望新框架包含一个文本字段和下拉菜单,以便用户可以选择一个选项。 Create node Search node
我有一个用户控件,用于编辑应用程序中的某些对象。 我最近遇到一个实例,我想弹出一个新的对话框(窗口)来托管此用户控件。 如何实例化新窗口并将需要设置的任何属性从窗口传递到用户控件? 感谢您的宝贵时间。
我有一个Observable,它发出许多对象,我想使用window或buffer操作对这些对象进行分组。但是,我不想指定count参数来确定窗口中应包含多少个对象,而是希望能够使用自定义条件。 例如,
我有以下代码,它打开一个新的 JavaFX 阶段(我们称之为窗口)。 openAlertBox.setOnAction(e -> { AlertBox alert = AlertBox
我要添加一个“在新窗口中打开”上下文菜单项,该菜单项将以新的UIScene打开我的应用程序文档之一。当然,我只想在实际上支持多个场景的设备上显示该菜单项。 目前,我只是在检查设备是否是使用旧设备的iP
我正在尝试创建一个 AIR 应用程序来记录应用程序的使用情况,使用 AIR 从系统获取信息的唯一简单方法是使用命令行工具和抓取 标准输出 . 我知道像 这样的工具顶部 和 ps 对于 OS X,但它们
所以我有这个简单的 turtle 螺旋制作器,我想知道是否有一种方法可以打印出由该程序创建的我的设计副本。 代码: import turtle x= float(input("Angle: ")) y
我正在编写一个 C# WPF 程序,它将文本消息发送到另一个程序的窗口。我有一个宏程序作为我的键盘驱动程序 (Logitech g15) 的一部分,它已经这样做了,尽管它不会将击键直接发送到进程,而是
我尝试使用以下代码通过 UDP 发送,但得到了奇怪的结果。 if((sendto(newSocket, sendBuf, totalLength, 0, (SOCKADDR *)&sendAd
我是一名优秀的程序员,十分优秀!