我分别在MouseEnter和MouseExit上添加和删除JButton.这工作正常,但当按钮添加到面板时,它会显示在右上角而不是BorderLayout.SOUTH指定的位置.
Frame只有一个JPanel,我添加的唯一一行是
jPanel1.addMouseListener(new myMouseListener(jPanel1));
Run Code Online (Sandbox Code Playgroud)
鼠标监听器
package example;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
public class myMouseListener extends MouseAdapter{
JButton btn;
JPanel panel;
public myMouseListener(JPanel panel)
{
this.panel = panel;
this.btn = new JButton("Test");
this.btn.setSize(40, 40);
}
public void mouseEntered(MouseEvent e) {
panel.setBackground(Color.red);
panel.add(btn, BorderLayout.SOUTH);
}
public void mouseExited(MouseEvent e) {
panel.setBackground(Color.blue);
panel.remove(btn);
}
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里下载一个sscce
http://www.filehosting.org/file/details/302851/Example.zip
任何人都可以对这个问题有所了解吗?
我添加JTable了一个JScrollPane然后添加到面板的滚动窗格,然后添加面板到框架,但它不起作用这里是代码.我想在桌子或框架上有滚动条,使桌子可滚动,以便用户可以看到它.我尝试了很多方法,但是对我来说没有工作的是整个代码
public class View extends JFrame {
private static final long serialVersionUID = 1L;
/**
* Launch the application.
*/
//here in the main method i it adds in the JFrame everything
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
View frame = new View();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void showData() throws SQLException, ParseException {
panel = new JPanel();
panel_1 = …Run Code Online (Sandbox Code Playgroud) 尝试简单地创建具有指定可见行数但具有最小宽度的JList.似乎"setMinimumSize()"没有做任何事情......
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UserInterface
{
final static private int HEIGHT = 400;
final static private int WIDTH = 650;
public static void main(String[] args) {
JPanel content = new JPanel();
String[] entries = { "Entry 1", "Entry 2", "Entry 3",
"Entry 4", "Entry 5", "Entry 6" };
DefaultListModel sampleModel = new DefaultListModel();
for(int i=0; i<entries.length; i++)
sampleModel.addElement(entries[i]);
JList sampleList = new JList(sampleModel);
sampleList.setMinimumSize(new Dimension(1000,1000));
sampleList.setMaximumSize(new Dimension(1000,1000));
content.add(sampleList);
//main window frame
JFrame window = new …Run Code Online (Sandbox Code Playgroud) 是否可以在Java中的一个方法中使用面板中的面板?
就像我在一个JPanel名为的方法中创建一个CreatePanel,我可以在它下面添加另一个吗?我试图在一个方法中添加两个或可能三个面板但到目前为止还没有成功.
如果这是不可能的,你会如何创建一个JPanel,比方说LeftPanel,并添加另一个JPanel内LeftPanel?
任何有关消息来源和明确解释的帮助都会非常棒,因为我是Java的初学者,有时当你说的话对你和所有的CS-jocks来说可能是显而易见的而不是我.
我有一个基本的gui(它不需要看起来漂亮,它只需要工作.显示我正在为我的论文实现的几种算法.)但是复选框和按钮不会随机显示.有时他们出现,有时他们不出现.老实说,我不知道为什么会发生这种情况,我很喜欢摇摆,所以我迷路了.
编辑:这是我想要我的gui看起来像:

//import java.awt.*;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import javax.swing.*; //notice javax
public class MapWindowController extends JFrame implements ActionListener, ItemListener
{
private static final int WIDTH = 600, HEIGHT = 800;
private static final int SETTINGS_WIDTH = 600, SETTINGS_HEIGHT = 200;
private static final int NUM_MAP_TYPE = 2;
private static final int NUM_ALGORITHM_TYPE = 4;
private static final int MAP_IMAGE_SIZE = 400, ACTUAL_MAP_SIZE = 10;
private JButton run;
private JCheckBox[] map_type;
private JCheckBox[] algorithm_type; …Run Code Online (Sandbox Code Playgroud) 我正在尝试用java swing创建一个棋盘游戏,但我对布局感到非常困惑.目前我正在尝试创建用户看到的图像.为了做到这一点,我使用了BorderLayout(我只需要使用Border-grid-flow布局),我认为我会在东南西北部放置一些工作人员,并在中心放置基本板.问题是我必须创建使用JButton移动棋子的路径(其余空间应该留空)并且该路径需要具有特定的十字形状:http://i59.tinypic.com/eileys.png和尺寸应该比平常小.对于初学者,我尝试使用带有gridlayout的JPanel(放入中心),但无论我做什么(如setPreferredSize()),按钮都会调整大小以填充空间.这是我的代码.感谢您的时间,任何提示都会有所帮助!编辑:
package projtest1;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Board extends JFrame
{
public Board()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Border Layout");
setMinimumSize(new Dimension(1280, 768));
setSize(1280, 768);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(new JButton("North"), BorderLayout.NORTH);
contentPane.add(new JButton("South"), BorderLayout.SOUTH);
contentPane.add(new JButton("West"), BorderLayout.WEST);
contentPane.add(new JButton("East"), BorderLayout.EAST);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 1, 1, 1));
buttonPanel.setSize(new Dimension (800,800));
JPanel topButtonPanel = new JPanel();
JPanel middleButtonPanel …Run Code Online (Sandbox Code Playgroud) 我同时将两个以上的问题,面临JPanel中的对象CardLayout ,它们在同一时间可见.虽然我可以放置一些面板,但它们不会同时出现,我无法按照自己的意愿调整它们.为了更清楚地了解我想要的内容,我添加了一个测试图像.

我创建了一个扩展类 JComponent并希望在它上面使用它JPanel.如果我设定界限,我就不能FlowLayout按照我想要的方式使用它; 它似乎JPanel是使用绝对定位.如果我没有指定边界,它根本不会出现.可能是什么解决方案?
谢谢.
JMotor.java:
public class JMotor extends JComponent {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
BufferedImage img = null;
try {
img = ImageIO.read(new File("motor.jpg"));
}
catch (Exception e) {
}
g2.drawImage(img, 5, 5, this);
//setBounds(5, 5, 100, 50);
}
}
Run Code Online (Sandbox Code Playgroud)
Scada.java:
public class Scada {
JFrame scadaFrame;
JPanel scadaPanel;
Scada () {
scadaFrame = new JFrame("Scada");
scadaFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scadaPanel = new JPanel();
scadaPanel.add(new JSuruculuMotor());
scadaPanel.add(new …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用GridBagLayout.我需要一个垂直和水平居中的JLabel - 这很容易,我甚至不需要创建任何GridBagConstraints.我还想把JButton放在右下角,当我尝试这样做时,我的中心面板向左移动或按钮向上移动.
EXPECTING GETTING THIS OR THIS
+-----------+ +-----------+ +-----------+
| | | | | |
| | | | | |
| | | | | |
| +---+ | | +---+ | | +---+ |
| | | | | | | | | | | |
| +---+ | | +---+ | | +---++---+|
| | | | | | ||
| | | | | +---+|
| +---+ | +---+ | |
| | | …Run Code Online (Sandbox Code Playgroud) 我有一个包含370个项目的交错网格,带有图像.
我想确保快速回收物品以注意内存,但是创建ViewHolder然后绑定到适配器中的每个项目,并且不关注子项是否可见
我尝试了以下内容
StaggeredGridLayoutManager lm = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
rv.setLayoutManager(lm);
rv.setItemViewCacheSize(20); //Has no effect
RecyclerView.RecycledViewPool pool = new RecyclerView.RecycledViewPool();
pool.setMaxRecycledViews(0, 20);
rv.setRecycledViewPool(pool); //also has no effect
Run Code Online (Sandbox Code Playgroud)
日志显示onCreateViewHolder和onBindViewHolder每次调用185次.然后在恢复对onCreateViewHolder的调用之前调用onViewRecycled 185次,直到我们达到完整的370.
这对我来说可能是一个理解问题,但我认为RecyclerView应该只绑定那些可见的视图,或者仅仅支持20个视图,或者20个池中的20个,但是很多适合屏幕.如何使用StaggeredGridLayoutManager实现此目的?
如果我听滚动更改并使用findFirstCompletelyVisibleItemPositions和findLastCompletelyVisibleItemPositions,这仍然跨越适配器中的每一项,而不仅仅是适合屏幕的6项
我的适配器代码
class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
static final int NUM_COLS = 3;
private final LayoutInflater mInflater;
private final List<GridItem> mEntries;
private int mLastExpanded; //stores where the last expanded item was
private OnCardClickListener mOnItemClick;
MyAdapter(Context context) {
super();
mEntries = new ArrayList<>();
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
void setOnTileClickListener(@Nullable OnCardClickListener listener) …Run Code Online (Sandbox Code Playgroud) android layout-manager staggeredgridlayout android-recyclerview
layout-manager ×10
java ×9
swing ×9
awt ×2
jpanel ×2
android ×1
button ×1
cardlayout ×1
flowlayout ×1
jbutton ×1
jlist ×1
jscrollpane ×1
jtable ×1