- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我希望我的应用程序看起来像这样:
我必须尝试使用
jpanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
jpanel.setAlignmentX(JPanel.RIGHT_ALIGNMENT);
但是当我将它添加到 main_JPanel 中时,它适用于第一个 jpanel。 Show correctly但是当我添加 JPanel2,JPanel3,...然后 main_Panel 显示不正确,它显示:
这是我的函数创建每个 JPanel
public JPanel makeJpanel(String Mess, String username) {
int height = 40;
int width = 200;
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
StyledDocument doc = textPane.getStyledDocument();
String rs = splitMess(Mess);
Mess = rs.substring(2).trim();
Style style = textPane.addStyle("I'm a Style", null);
StyleConstants.setFontFamily(style, "Arial");
StyleConstants.setBold(style, true);
StyleConstants.setFontSize(style, 20);
StyleConstants.setForeground(style, new Color(19, 51, 55));
try {
doc.insertString(doc.getLength(), Mess.trim(), style);
} catch (BadLocationException e) {
}
height = (int) Math.round(textPane.getPreferredSize().getHeight());
width = (int) Math.round(textPane.getPreferredSize().getWidth());
//creat avatar
JLabel ava = null;
if (username.equals(this.userName)) {
ava = new JLabel(label_myAvaProfile1.getIcon());
} else {
ava = new JLabel(label_avaYourFriends1.getIcon());
}
JLabel borderAva = new JLabel(border_label_avaYourFriends.getIcon());
if (height < 40) {
ava.setPreferredSize(new Dimension(40, 40));
borderAva.setPreferredSize(new Dimension(40, 40));
} else {
ava.setPreferredSize(new Dimension(40, height));
borderAva.setPreferredSize(new Dimension(40, height));
}
JLayeredPane lpAva = new JLayeredPane();
lpAva.setLayout(new OverlayLayout(lpAva));
lpAva.add(borderAva);
lpAva.add(ava);
JPanel panelChat = new JPanel();
panelChat.setLayout(new BoxLayout(panelChat, BoxLayout.LINE_AXIS));
panelChat.setBackground(Color.WHITE);
panelChat.setPreferredSize(new Dimension(width + 40, height));
panelChat.setMinimumSize(new Dimension(width + 40, height));
panelChat.setMaximumSize(new Dimension(width + 40, height));
if (username.equals(this.userName)) {
panelChat.add(textPane);
panelChat.add(lpAva);
//NOTICE THIS
panelChat.setAlignmentX(JPanel.LEFT_ALIGNMENT);
} else if (username.equals(currentFriendUserName)) {
panelChat.add(lpAva);
panelChat.add(textPane);
//NOTICE THIS
panelChat.setAlignmentX(JPanel.RIGHT_ALIGNMENT);
}
resetPanelListFriends(panelChat);
return panelChat;
}
当我添加新的 Jpanel 时我调用这个函数
public void renderPanelChatLog()
{
JPanel panelChat = new JPanel();
panelChat = makeJPanel(mess, username);
if (panelChat != null) {
panel_ChatLog.add(panelChat);
panel_ChatLog.add(Box.createVerticalStrut(20));
}
resetPanelListFriends(panel_ChatLog);
}
对不起,我的英语,我只是个新人......请帮助我!
最佳答案
这是一种方法:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class BoxLayoutAligned
{
private static void createAndShowGUI()
{
JLabel left = new JLabel( "Left Aligned" );
Box leftBox = Box.createHorizontalBox();
leftBox.add( left );
leftBox.add( Box.createHorizontalGlue() );
JLabel right = new JLabel( "Right Aligned" );
Box rightBox = Box.createHorizontalBox();
rightBox.add( Box.createHorizontalGlue() );
rightBox.add( right );
Box vertical = Box.createVerticalBox();
vertical.setBorder( new EmptyBorder(0, 10, 0, 10) );
vertical.add( leftBox );
vertical.add( rightBox );
JFrame frame = new JFrame("BoxLayout Aligned");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(vertical);
frame.setSize(300, 300);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
}
}
通过添加“粘合”,您可以将添加到 Box 的其他组件向左或向右对齐。
关于java - 如何将每个 JPanel 向左或向右对齐到主 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60657134/
在 Vim 中,是否可以将窗口“移动”到左侧或右侧?例如,类似于 r或 x ,但是左/右而不是上/下? 例如,如果我有这个布局: +---+---+---+ | | +---+ | A +--
我有一系列枚举的网页,目前每个页面上都有一个向左和向右箭头图标,用于导航到该系列中的上一页/下一页。 我的问题:实现向左/向右滑动手势以执行相同操作的最简单(!)方法是什么? (没有其他手势 - 请不
在我的网站上,我有三个 div、一个标题、一个主要内容和右侧的侧边栏。侧边栏是实际问题。我希望侧边栏始终位于浏览器窗口的右侧,如果用户调整窗口大小使宽度低于主栏和侧边栏,则侧边栏会停在主要内容的左边缘
请访问http://lindseymotors.com/v然后单击银色卡车顶部的照片。当展开的 View 弹出时,主照片下方有一排照片。当您点击它们时,它会切换到该照片并在您选择的图像上方显示“正在查
请看一下这个 DEMO 。 使用 this post 中的代码我已成功通过单击左侧或右侧链接使图像水平移动。 我想要实现的是以某种方式使图像跳跃更平滑。 这是我的代码: var scroll
我正在尝试向子菜单添加一个不错的小效果,但我不能使用 marginLeft,因为它会弄乱容器。所以我试图通过使用 left 而不是 marginLeft 来实现它,但我无法让它工作......任何想法
我有一个日历,我想让左右滑动和切换月份成为可能。 是否有任何听众向左/向右滑动? 谢谢! 最佳答案 您可以像下面的示例一样使用 SimpleOnGestureListener:(显然,将 toast
我正在尝试实现一个ng-repeats 水平用户媒体对象的小部件,我应该能够向左/向右滑动以显示下一张媒体卡。小部件应该看起来像这样 如图所示 下一张卡片头像是半可见的,暗示还有更多卡片可以刷。 这就
我正在创建一个显示多个“页面”内容的应用程序。通常,这代表基于桌面的应用程序上的选项卡。 在 android 上,我希望能够在屏幕上的任意位置使用手指在信息选项卡之间轻拂。这些选项卡的内容将只是 XM
我正在使用: ViewController = [[ViewControllerClass alloc] initWithNibName:@"ViewControllerClass" bundle:[
这个问题已经存在: How to make this into a sliding left/right div 已关闭10 年前。 下面提供的是我的 html 和 css 代码的片段,我不仅需要在
尝试将 h3 向左滑动,p 向右滑动,.info 从下到上滑动 slider .实际上它在没有 jQuery 的 Chrome 和 Opera 中工作得很好,因为我使用了 Animate.css,但它
我发现这个很好的进度条 CSS 动画。 但我想transform: translate而不是left/right。 如何切换到 transform 它?我试过了,但它不起作用: https://cod
我想用 bootstrap 创建一个表,并在列中插入一些左拉和右拉的内容: http://jsfiddle.net/Zoker/sgdfgkvL/ Some content Some
我怎样才能得到 重叠并停在 Logo 的左边缘?当屏幕宽度扩展时,我试图让绿色条向左扩展,但我希望它停在 Logo 的左边缘。 我试过了 position: absolute;在 #green_bar
试图将这个“手势”功能添加到我的第一个程序中,几乎我所做的每一次搜索都来到了这个线程: Fling gesture detection on grid layout 我能够让它工作..但就我而言,我不
我有带 tablayout 的 viewpager,在 ViewPager 内部我有一个正在创建 RecyclerView 的 fragment , 在 RecyclerVIew roe 项目中,我创
我一直在研究图片库 [html5],它在桌面版本中运行良好,我想为 Ipad/平板电脑设备添加基于触摸的事件。 您能否建议如何使用 javascript/jquery 添加基于触摸的事件。 谢谢,斯里
所以我想通过将盒子相乘然后去掉下半部分来使盒子向各个方向移动。向上和向右有效,但向下和向左无效。单击 4 个按钮中的 1 个即可调用这些功能。基本的 CSS 和 HTML。我该如何解决这个问题? va
我已经像这样创建了一个 RecyclerView ItemTouchHelper - public class MyItemTouchHelper extends android.support.v7
我是一名优秀的程序员,十分优秀!