Java 网格布局错误的按钮大小和错误的位置
Java Grid Layout wrong Button size and wrong location
我遇到了 Java Swing 应用程序的问题。
我的代码:
public class AdminToken extends JFrame {
VirtualServer server;
@Override
public Container getContentPane() {
return super.getContentPane();
}
public AdminToken(VirtualServer server)
{
this.server = server;
getContentPane().setLayout(null);
setupGUI();
setLayout(new GridLayout(5,1));
}
void setupGUI()
{
setTitle("Token Management Server: " + server.getName() );
setSize(576,528);
setVisible(true);
setResizable(true);
final JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5,1));
LoginForm.api.selectVirtualServer(server);
for( final PrivilegeKey tokens : LoginForm.api.getPrivilegeKeys()) {
final JLabel label = new JLabel(tokens.getToken() + " " + tokens.getGroupId() + " " + "\n");
panel.add(label);
JButton button = new JButton("Löschen");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginForm.api.deletePrivilegeKey(tokens.getToken());
panel.remove(label);
panel.repaint();
}
});
button.setVisible(true);
button.setPreferredSize(new Dimension(87,25));
panel.add(button);
}
JScrollPane scrollPane = new JScrollPane (panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane);
LoginForm.api.selectVirtualServerById(LoginForm.vServerId);
}
代币应该这样显示:
Token1 按钮删除\n
令牌 2 按钮删除\n
令牌 3 按钮删除\n
还有一个
而且我也无法在另一个 JFrame 中更改按钮的大小:
代码:
public class TsServerOverview extends JFrame {
@Override
public Container getContentPane() {
return super.getContentPane();
}
public TsServerOverview()
{
getContentPane().setLayout(null);
setupGUI();
setLayout(new GridLayout());
}
void setupGUI()
{
setTitle("Ts3Query Server Oveview");
setSize(576,528);
setVisible(true);
setResizable(true);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5,2));
for( final VirtualServer servers : LoginForm.api.getVirtualServers()) {
final int id = servers.getId();
panel.add(new JLabel(servers.getId() + " " + servers.getName() + " " + servers.getClientsOnline() + "/" + servers.getMaxClients() + "\n"));
JButton button = new JButton("Konsole öffnen");
JButton showchannels = new JButton("Channel-Liste");
JButton tokenmanagement = new JButton("Token Management");
showchannels.setPreferredSize(new Dimension(10,10));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new GUIConsole(servers);
}
});
showchannels.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ChannelList(servers);
}
});
tokenmanagement.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AdminToken(servers);
}
});
tokenmanagement.setVisible(true);
button.setVisible(true);
showchannels.setVisible(true);
panel.add(showchannels);
panel.add(button);
panel.add(tokenmanagement);
}
JScrollPane scrollPane = new JScrollPane (panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane);
}
那也应该这样显示:
Server1 3/32 联机启动停止控制台\n
Server2 3/32 脱机启动停止控制台\n
Server3 3/32 联机启动停止控制台
按钮应该在服务器行旁边。
如果有人可以帮助我编写代码,那就太好了。
public class AdminToken extends JFrame {
VirtualServer server;
@Override
public Container getContentPane() {
return super.getContentPane();
}
删除所有不需要证明问题的内容。其中包括 VirtualServer
public AdminToken(VirtualServer server)
{
this.server = server;
getContentPane().setLayout(null);
setupGUI();
setLayout(new GridLayout(5,1));
}
getContentPane().setLayout
和setLayout
本质上是一样的。设置两次没用。避免空布局管理器。
void setupGUI()
{
setTitle("Token Management Server: " + server.getName() );
setSize(576,528);
setVisible(true);
setResizable(true);
setTitle
不需要证明该问题。无需设置大小。在适用时设置首选尺寸,让布局管理器进行布局。 setResizable(true)
是默认值。同样适用于 button.setVisible(true);
您通常希望 setVisible(true);
在末尾。
final JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5,1));
通过 new GridLayout(5,1)
您将网格设置为有 5 行。如果你添加更多它会弄乱布局。来自 java 文档:
rows - the rows, with the value zero meaning any number of rows.
LoginForm.api.selectVirtualServer(server);
for( final PrivilegeKey tokens : LoginForm.api.getPrivilegeKeys()) {
final JLabel label = new JLabel(tokens.getToken() + " " + tokens.getGroupId() + " " + "\n");
panel.add(label);
JButton button = new JButton("Löschen");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginForm.api.deletePrivilegeKey(tokens.getToken());
panel.remove(label);
panel.repaint();
}
});
button.setVisible(true);
button.setPreferredSize(new Dimension(87,25));
panel.add(button);
}
JScrollPane scrollPane = new JScrollPane (panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane);
LoginForm.api.selectVirtualServerById(LoginForm.vServerId);
}
这里 mcve 展示了我认为您要实现的目标:
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class AdminToken extends JFrame {
public AdminToken()
{
setupGUI();
setVisible(true);
setResizable(true);
pack();
}
void setupGUI() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0,2));
for(int row =0; row < 6; row++ ) {
panel.add(new JLabel("Label "+row));
panel.add(new JButton("Delete"));
}
JScrollPane scrollPane = new JScrollPane (panel);
add(scrollPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new AdminToken() );
}
}
我遇到了 Java Swing 应用程序的问题。
我的代码:
public class AdminToken extends JFrame {
VirtualServer server;
@Override
public Container getContentPane() {
return super.getContentPane();
}
public AdminToken(VirtualServer server)
{
this.server = server;
getContentPane().setLayout(null);
setupGUI();
setLayout(new GridLayout(5,1));
}
void setupGUI()
{
setTitle("Token Management Server: " + server.getName() );
setSize(576,528);
setVisible(true);
setResizable(true);
final JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5,1));
LoginForm.api.selectVirtualServer(server);
for( final PrivilegeKey tokens : LoginForm.api.getPrivilegeKeys()) {
final JLabel label = new JLabel(tokens.getToken() + " " + tokens.getGroupId() + " " + "\n");
panel.add(label);
JButton button = new JButton("Löschen");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginForm.api.deletePrivilegeKey(tokens.getToken());
panel.remove(label);
panel.repaint();
}
});
button.setVisible(true);
button.setPreferredSize(new Dimension(87,25));
panel.add(button);
}
JScrollPane scrollPane = new JScrollPane (panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane);
LoginForm.api.selectVirtualServerById(LoginForm.vServerId);
}
代币应该这样显示:
Token1 按钮删除\n
令牌 2 按钮删除\n
令牌 3 按钮删除\n 还有一个
而且我也无法在另一个 JFrame 中更改按钮的大小:
代码:
public class TsServerOverview extends JFrame {
@Override
public Container getContentPane() {
return super.getContentPane();
}
public TsServerOverview()
{
getContentPane().setLayout(null);
setupGUI();
setLayout(new GridLayout());
}
void setupGUI()
{
setTitle("Ts3Query Server Oveview");
setSize(576,528);
setVisible(true);
setResizable(true);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5,2));
for( final VirtualServer servers : LoginForm.api.getVirtualServers()) {
final int id = servers.getId();
panel.add(new JLabel(servers.getId() + " " + servers.getName() + " " + servers.getClientsOnline() + "/" + servers.getMaxClients() + "\n"));
JButton button = new JButton("Konsole öffnen");
JButton showchannels = new JButton("Channel-Liste");
JButton tokenmanagement = new JButton("Token Management");
showchannels.setPreferredSize(new Dimension(10,10));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new GUIConsole(servers);
}
});
showchannels.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ChannelList(servers);
}
});
tokenmanagement.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AdminToken(servers);
}
});
tokenmanagement.setVisible(true);
button.setVisible(true);
showchannels.setVisible(true);
panel.add(showchannels);
panel.add(button);
panel.add(tokenmanagement);
}
JScrollPane scrollPane = new JScrollPane (panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane);
}
那也应该这样显示:
Server1 3/32 联机启动停止控制台\n
Server2 3/32 脱机启动停止控制台\n
Server3 3/32 联机启动停止控制台
按钮应该在服务器行旁边。
如果有人可以帮助我编写代码,那就太好了。
public class AdminToken extends JFrame {
VirtualServer server;
@Override
public Container getContentPane() {
return super.getContentPane();
}
删除所有不需要证明问题的内容。其中包括 VirtualServer
public AdminToken(VirtualServer server)
{
this.server = server;
getContentPane().setLayout(null);
setupGUI();
setLayout(new GridLayout(5,1));
}
getContentPane().setLayout
和setLayout
本质上是一样的。设置两次没用。避免空布局管理器。
void setupGUI()
{
setTitle("Token Management Server: " + server.getName() );
setSize(576,528);
setVisible(true);
setResizable(true);
setTitle
不需要证明该问题。无需设置大小。在适用时设置首选尺寸,让布局管理器进行布局。 setResizable(true)
是默认值。同样适用于 button.setVisible(true);
您通常希望 setVisible(true);
在末尾。
final JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5,1));
通过 new GridLayout(5,1)
您将网格设置为有 5 行。如果你添加更多它会弄乱布局。来自 java 文档:
rows - the rows, with the value zero meaning any number of rows.
LoginForm.api.selectVirtualServer(server);
for( final PrivilegeKey tokens : LoginForm.api.getPrivilegeKeys()) {
final JLabel label = new JLabel(tokens.getToken() + " " + tokens.getGroupId() + " " + "\n");
panel.add(label);
JButton button = new JButton("Löschen");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginForm.api.deletePrivilegeKey(tokens.getToken());
panel.remove(label);
panel.repaint();
}
});
button.setVisible(true);
button.setPreferredSize(new Dimension(87,25));
panel.add(button);
}
JScrollPane scrollPane = new JScrollPane (panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane);
LoginForm.api.selectVirtualServerById(LoginForm.vServerId);
}
这里 mcve 展示了我认为您要实现的目标:
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class AdminToken extends JFrame {
public AdminToken()
{
setupGUI();
setVisible(true);
setResizable(true);
pack();
}
void setupGUI() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0,2));
for(int row =0; row < 6; row++ ) {
panel.add(new JLabel("Label "+row));
panel.add(new JButton("Delete"));
}
JScrollPane scrollPane = new JScrollPane (panel);
add(scrollPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new AdminToken() );
}
}